forked from yychuyu/githubTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_ValidParentheses.py
More file actions
27 lines (26 loc) · 1010 Bytes
/
20_ValidParentheses.py
File metadata and controls
27 lines (26 loc) · 1010 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
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s) == 0:
return True
elif len(s) % 2 == 1 or s[0] == ")" or s[0] == "]" or s[0] == "}":
return False
else:
d = {"(": ")", "[": "]", "{": "}"}
tmp = s[0]
for parenthese in s[1:]:
tmp = tmp + parenthese
# if parenthese != d[tmp[-1]] or parenthese != "(" or parenthese != "[" or parenthese != "{":
# return False
#len(tmp)>=2是为了防止诸如()[]的情况,{}消除后第一个tmp长度只有1,程序中断
if len(tmp) >= 2 and parenthese == d[tmp[-2]]:
tmp = tmp[:-2]
elif parenthese != "(" and parenthese != "[" and parenthese != "{":
return False
if len(tmp) == 0:
return True
else:
return False