-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.py
More file actions
43 lines (39 loc) · 1.14 KB
/
20.py
File metadata and controls
43 lines (39 loc) · 1.14 KB
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
34
35
36
37
38
39
40
41
42
43
class Solution(object):
# def isValid(self, s):
# """
# :type s: str
# :rtype: bool
# """
# l = []
# for i in s:
# if i is '(' or i is '[' or i is '{':
# l.append(i)
# if i is ')' and len(l)>0 and l[-1] is '(':
# l.pop()
# if i is ']' and len(l)>0 and l[-1] is '[':
# l.pop()
# if i is '}' and len(l)>0 and l[-1] is '{':
# l.pop()
# if len(l)>0:
# return False
# return True
def isValid(self, s):
stack = []
for i in range(len(s)):
if s[i] == '(' or s[i] == '[' or s[i] == '{':
stack.append(s[i])
if s[i] == ')':
if stack == [] or stack.pop() != '(':
return False
if s[i] == ']':
if stack == [] or stack.pop() != '[':
return False
if s[i] == '}':
if stack == [] or stack.pop() != '{':
return False
if stack:
return False
else:
return True
test = Solution()
print test.isValid("()")