-
Notifications
You must be signed in to change notification settings - Fork 0
/
020_validParentheses.py
56 lines (45 loc) · 1.75 KB
/
020_validParentheses.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
53
54
55
56
#!/usr/bin/env python
#coding:utf-8
#@author: rye
#@time: 2019/2/15 11:04
'''
虽然是道简单题,但是因为目前还没用过栈,所以还没会,看了官方解题步骤https://leetcode-cn.com/problems/valid-parentheses/solution/
知道怎么做了
tips:
1. 学习该题的解题思路
2. 学习使用栈
3. 学习使用hashmap!
'''
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
# The stack to keep track of opening brackets.
stack = []
# Hash map for keeping track of mappings. This keeps the code very clean.
# Also makes adding more types of parenthesis easier
mapping = {")": "(", "}": "{", "]": "["}
# For every bracket in the expression.
for char in s:
# If the character is an closing bracket
if char in mapping:
# Pop the topmost element from the stack, if it is non empty
# Otherwise assign a dummy value of '#' to the top_element variable
top_element = stack.pop() if stack else '#'
# The mapping for the opening bracket in our hash and the top
# element of the stack don't match, return False
if mapping[char] != top_element:
return False
else:
# We have an opening bracket, simply push it onto the stack.
stack.append(char)
# In the end, if the stack is empty, then we have a valid expression.
# The stack won't be empty for cases like ((()
return not stack
if __name__ == '__main__':
s1 = "()[]{}"
s2 = "(]"
s3 = '()'
print(Solution().isValid(s3))