leetcode 20. Valid Parentheses
2017-10-13 17:18
513 查看
在学习数据结构中看到栈,因此回想起之前做leetcode时遇到的一题:
Given a string containing just the characters
The brackets must close in the correct order,
class Solution(object):
def isValid(self, s):
mystack = []
for item in s:
if item in ["(","[","{"]:
mystack.append(item)
if item in [")","]","}"]:
if mystack == []:
return False
top = mystack[-1]
if (top == '(' and item == ')') or (top == "[" and item == "]") or (top == "{" and item == "}"):
mystack.pop()
else:
return False
return len(mystack) == 0
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):
mystack = []
for item in s:
if item in ["(","[","{"]:
mystack.append(item)
if item in [")","]","}"]:
if mystack == []:
return False
top = mystack[-1]
if (top == '(' and item == ')') or (top == "[" and item == "]") or (top == "{" and item == "}"):
mystack.pop()
else:
return False
return len(mystack) == 0
相关文章推荐
- Leetcode_20_Valid Parentheses
- LeetCode 20. Valid Parentheses
- 【Leetcode】20. Valid Parentheses
- Leetcode 20 Valid Parentheses
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
- leetcode20:Valid Parentheses
- 【LeetCode】20_Valid Parentheses
- leetcode 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- leetcode 20 -- Valid Parentheses
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
- 20. Valid Parentheses LeetCode
- Leetcode[20]-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-每日编程第三十七题
- [Leetcode 20, Easy] Valid Parentheses