【LEETCODE】20-Valid Parentheses
来源:互联网 发布:java杨辉三角几年里 编辑:程序博客网 时间:2024/06/15 11:28
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.
思路:
遇到左括号,入栈
遇到了右括号,如果此时栈顶元素是相应的左括号,则True,其他情况为False
遇到一对后,弹出
[ ( ) ]
( ) [ ] { }
class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ stack=[] for i in range(len(s)): if s[i] == '(' or s[i] == '[' or s[i] == '{': stack.append(s[i]) if s[i] == ')': if stack == [] or stack.pop() != '(': return False if s[i] == ']': if stack == [] or stack.pop() != '[': return False if s[i] == '}': if stack == [] or stack.pop() != '{': return False if stack: return False else: return True
0 0
- 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 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 - Valid Parentheses
- LeetCode 20: Valid Parentheses
- leetcode-20 Valid Parentheses
- leetcode 20 -- Valid Parentheses
- Oracle12c新特性之基本操作
- (数据挖掘-入门-4)基于物品的协同过滤
- 【初面实习】爱奇艺测试实习生面试总结
- Spring 3整合Quartz 2实现定时任务--转
- (数据挖掘-入门-5)基于内容的协同过滤与分类
- 【LEETCODE】20-Valid Parentheses
- 第十五周 项目1 插入排序
- 配置NGINX的ACCESS LOG
- 第12周项目1图基本算法库
- strtotime小笔记
- (数据挖掘-入门-6)十折交叉验证和K近邻
- 第十三周 Floyd 算法
- 第16周 项目1-交换排序之冒泡排序
- 自定义属性