forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_parentheses.py
More file actions
27 lines (25 loc) · 912 Bytes
/
valid_parentheses.py
File metadata and controls
27 lines (25 loc) · 912 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
# -*- coding: utf-8 -*-
class Solution:
# @param {string} s A string
# @return {boolean} whether the string is a valid parentheses
def isValidParentheses(self, s):
# Write your code here
par_list = []
for ch in s:
if (ch == '(') or (ch == '[') or (ch == '{'):
par_list.append(ch) # 插入左括号
else:
if not self.check(ch, par_list):
return False
return True if not par_list else False
def check(self, right, par_list):
if not par_list:
return False
left = par_list[-1]
if ((left == '(') and (right == ')')) or \
((left == '[') and (right == ']')) or \
((left == '{') and (right == '}')):
par_list.pop() # 括号匹配成功后弹出
else:
par_list.append(right)
return True