leetcode: 20. Valid Parentheses
来源:互联网 发布:sql注入入门 编辑:程序博客网 时间:2024/05/21 06:34
Problem
# 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.
AC
class Solution(): def isValid(self, x): d, lst = {"(":")", "[":"]", "{":"}"}, [] for ele in x: if ele in d.keys(): # prefix: push lst.append(ele) elif not (lst and d[lst.pop()] == ele): # suffix: pop, then if cannot match, return False return False return len(lst) == 0if __name__ == "__main__": assert Solution().isValid("()[]{}") == True assert Solution().isValid("()[{]}") == False assert Solution().isValid("[") == False assert Solution().isValid("]") == False
阅读全文
2 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
- 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. Valid Parentheses
- Linux centos7下MySQL的安装
- 网络编程—socket编程
- 文本配置无需编程的网页爬取框架Spider123
- android移植osgearth+依赖库
- 九九乘法表
- leetcode: 20. Valid Parentheses
- NLP10-Gensim源码简析[LsiModel]
- 时间规模为nlogn的数列逆序对数量计算
- 第二章 SQL命令参考-REASSIGN OWNED
- Android SDK在线 国内源
- 6.1(2)
- oracle时间函数
- windows下通过IDE发布job到Hadoop Linux集群
- leetcode: 21. Merge Two Sorted Lists