[题解][LeetCode][Valid Parentheses]
来源:互联网 发布:淘宝严重违规12分2999 编辑:程序博客网 时间:2024/04/30 17:22
题目:
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.
题解:
【字符串处理】
用一个List当作堆栈,遇到左括号压栈,遇到右括号弹栈比较。
最后check一下栈是否为空即可。
Code:
class Solution:# @return a booleandef isValid(self, s):p = []l = ['(','[','{']r = [')',']','}']for c in s:if c in l:p.append(c)elif c in r:if p == []:return Falseif p.pop() != l[r.index(c)]:return Falseif p == []:return Trueelse:return False
0 0
- LeetCode题解:Valid Parentheses
- [题解][LeetCode][Valid Parentheses]
- [leetcode]Valid Parentheses题解
- LeetCode题解:Valid Parentheses
- LeetCode题解:Valid Parentheses
- LeetCode题解:Longest Valid Parentheses
- [leetcode]Longest Valid Parentheses题解
- leetcode 题解 || Valid Parentheses 问题
- LeetCode题解:Longest Valid Parentheses
- leetcode题解-20. Valid Parentheses
- LeetCode 题解(78): Valid Parentheses
- LeetCode 题解(111): Longest Valid Parentheses
- LeetCode题解——Valid Parentheses
- Leetcode题解14 20. Valid Parentheses
- LeetCode Algorithms 32. Longest Valid Parentheses 题解
- leetcode题解-32. Longest Valid Parentheses
- Valid Parentheses python题解
- 20. Valid Parentheses题解
- Linux下启动Tomcat
- HDFS:NameNode、DataNode、SecondaryNameNode
- Linux Thread 最基本用法
- LeetCode-Sum Root to Leaf Numbers
- 再谈冒烟测试
- [题解][LeetCode][Valid Parentheses]
- [leetcode] Unique Paths
- LeetCode: Validate Binary Search Tree
- 解密 【JavaScript Eval】不为人知的秘密
- IOS开发之__bridge,__bridge_transfer和__bridge_retained
- 初学ICE中间件总结笔记(一)
- 总结安卓开发中常用的七个第三方框架
- day122(7.13)性能测试知识总结V
- Enterprise Architect使用问题记录