-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.py
53 lines (35 loc) · 942 Bytes
/
20.py
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
44
45
46
47
48
49
50
51
52
#堆栈,不多说了 类似计算器的实现
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
searchfor = {
'(':'1',
'[':'2',
'{':'3',
'}':'{',
']':'[',
')':'('
}
length = len(s)
list = []
for i in range(length):
if len(list)==0:
list.append(s[i])
continue
up=s[i]
down =list.pop()
if searchfor[up]==down:
continue
else:
list.append(down)
list.append(up)
if list == []:
return True
else:
return False
if __name__ =="__main__":
sl =Solution()
print(sl.isValid("()[]{}"))