Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 020_Valid_Parentheses.py #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 11 additions & 25 deletions python/020_Valid_Parentheses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,24 @@ def isValid(self, s):
:type s: str
:rtype: bool
"""
if s is None:
return True
pairs={"(":")","[":"]","{":"}"}
stack = []
for t in s:
if t == ')':
try:
current = stack.pop()
if current != '(':
return False
except:
return False
elif t == '}':
for i in range(len(s)):
if s[i] in pairs.keys():
stack.append(s[i])
else :
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove space before :

try:
current = stack.pop()
if current != '{':
if pairs[stack[-1]] == s[i]:
stack.pop()
else:
return False
except:
return False
elif t == ']':
try:
current = stack.pop()
if current != '[':
return False
except:
return False
else:
stack.append(t)
if len(stack) == 0:
if stack == []:
return True
else:
else :
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove space before :

return False

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty spaces.


# def isValid(self, s):
# # python replace
Expand Down