-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path020.py
31 lines (28 loc) · 823 Bytes
/
020.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
#==================================================
#==> Title: valid-parentheses
#==> Author: Zhang zhen
#==> Email: hustmatnoble.gmail.com
#==> GitHub: https://github.com/MatNoble
#==> Date: 1/15/2021
#==================================================
"""
stack
https://leetcode-cn.com/problems/valid-parentheses/
"""
class Solution:
def isValid(self, s):
dict = {'(':1, ')':-1, '{':2, '}':-2, '[':3, ']':-3}
stack = []
for val in s:
temp = dict[val]
if len(stack) != 0 and stack[-1] > 0 and stack[-1] + temp == 0:
stack.pop()
else:
stack.append(temp)
return True if len(stack) == 0 else False
s = "()[]{)"
s = ")("
s = "()"
s = "([}}])"
mat = Solution()
mat.isValid(s)