LeetCode: 20. Valid Parentheses
2016-10-14 17:31
369 查看
题目:
Given a string containing just the characters
determine if the input string is valid.
The brackets must close in the correct order,
all valid but
not.
题意:
配对括号是否一致
题解:
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
cur=[]
count=0
for i in range(len(s)):
if s[i] in [')', ']', '}'] and count==0:
return False
if s[i] in ['(', '[', '{']:
cur.append(s[i])
count=count+1
elif (count>0) and (s[i]==')' and cur[count-1]=='(') or (s[i]==']' and cur[count-1]=='[') or (s[i]=='}' and cur[count-1]=='{'):
del cur[count-1]
count=count-1
else:
return False
if count==0:
return True
else:
return False
Given a string containing just the characters
'(',
')',
'{',
'}',
'['and
']',
determine if the input string is valid.
The brackets must close in the correct order,
"()"and
"()[]{}"are
all valid but
"(]"and
"([)]"are
not.
题意:
配对括号是否一致
题解:
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
cur=[]
count=0
for i in range(len(s)):
if s[i] in [')', ']', '}'] and count==0:
return False
if s[i] in ['(', '[', '{']:
cur.append(s[i])
count=count+1
elif (count>0) and (s[i]==')' and cur[count-1]=='(') or (s[i]==']' and cur[count-1]=='[') or (s[i]=='}' and cur[count-1]=='{'):
del cur[count-1]
count=count-1
else:
return False
if count==0:
return True
else:
return False
相关文章推荐
- leetcode[20]:Valid Parentheses
- [Leetcode] 20. Valid Parentheses(Stack)
- 【LeetCode】20. Valid Parentheses
- [Leetcode] 20. Valid Parentheses
- Leetcode: 20. Valid Parentheses
- [LeetCode]20. Valid Parentheses
- [leetcode]20. Valid Parentheses(java)
- LeetCode(20)Valid Parentheses
- leetcode之top100_01_twoSsum/top100_20_ValidParentheses
- [LeetCode-Algorithms-20] "Valid Parentheses" (2017.11.1-WEEK9)
- LeetCode 20.Valid Parentheses
- LeetCode20——Valid Parentheses
- LeetCode 20 Valid Parentheses
- leetcode-20. Valid Parentheses
- 【LeetCode】20. Valid Parentheses
- LeetCode-20-Valid Parentheses(有效的括号)
- leetcode解题报告20. Valid Parentheses
- leetcode_20_Valid Parentheses (easy)
- LeetCode-20. Valid Parentheses
- LeetCode 20 -- Valid Parentheses