-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20. Valid Parentheses.py
More file actions
33 lines (32 loc) · 897 Bytes
/
20. Valid Parentheses.py
File metadata and controls
33 lines (32 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = ['0']
for i in range(0,len(s)):
if s[i] == '(' or s[i] == '{' or s[i] == '[':
stack.append(s[i])
elif s[i] == ')':
if stack[-1] == '(':
stack.pop()
else:
return False
elif s[i] == '}':
if stack[-1] == '{':
stack.pop()
else:
return False
elif s[i] == ']':
if stack[-1] == '[':
stack.pop()
else:
return False
if len(stack) == 1:
return True
return False
if __name__ == '__main__':
solution = Solution()
re = solution.isValid("]")
print re