-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path20. Valid Parentheses.py
More file actions
40 lines (34 loc) · 890 Bytes
/
20. Valid Parentheses.py
File metadata and controls
40 lines (34 loc) · 890 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
34
35
36
37
38
39
40
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
CharDict = {"(": 1, ")": 19, "[": 2, "]": 18, "{": 3, "}": 17}
stack = []
ans = True
for i, ch in enumerate(s):
if CharDict[ch] < 10:
stack += [CharDict[ch]]
else:
if stack == [] or CharDict[ch] + stack[-1] != 20:
ans = False
break
else:
stack.pop()
if stack != []:
ans = False
return ans
import time
start = time.clock()
a= "{[]}"
ExpectOutput = True
solu = Solution() # 先对类初始化,才能进行调用
temp = solu.isValid(a)
if (temp == ExpectOutput):
print('right')
else:
print('wrong')
print(temp)
elapsed = (time.clock() - start)
print("Time used:", elapsed)