[勇者闯LeetCode] 20. Valid Parentheses
来源:互联网 发布:校园网络连接 编辑:程序博客网 时间:2024/05/20 06:27
[勇者闯LeetCode] 20. Valid Parentheses
Description
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.
Information
- Tags: Stack | String
- Difficulty: Easy
Solution
用栈存储需要遇到的右括号。扫描字符串时遇到左括号则其对应的右括号入栈,遇到右括号则出栈并判断两个右括号是否相同,若不相同则返回false
。扫描结束后,若栈内还有元素,则返回false
。
class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ l = {"(": ")", "[": "]", "{": "}"} ans = [] for x in s: if x in l: ans.append(l[x]) elif (len(ans) == 0 or ans.pop() != x): return False return False if len(ans) > 0 else True
0 0
- [勇者闯LeetCode] 20. Valid Parentheses
- [勇者闯LeetCode] 125. Valid Palindrome
- 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
- 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
- viewPager来实现fragment滑动效果
- 快讯:中央巡视组进驻北京大学公告
- 004.双链循环线性表C实例
- LCD驱动程序(3)显存与调色板
- TJOI2016&HEOI2016 排序 线段树+二分答案
- [勇者闯LeetCode] 20. Valid Parentheses
- 使用KindEditor插件 报错Uncaught ReferenceError: KindEditor is not defined
- JSP九大内置对象
- POJ4015:邮箱验证
- Linux(1):安装Ubuntu(在Windows下装Linux)
- Angular2中使用ngx-translate进行国际化
- window下安装git
- 美团点评SQL优化工具SQLAdvisor开源
- Linux下配置自己的Git服务器