Leetcode-Valid Parentheses-Python
来源:互联网 发布:php的csrf防护 编辑:程序博客网 时间:2024/05/21 06:23
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.
解题思路:
采用栈来解决,依次检查给的characters,如果是左括号都入栈;如果是右括号,检查栈如果为空,证明不能匹配,如果栈不空,弹出top,与当前扫描的括号检查是否匹配。全部字符都检查完了以后,判断栈是否为空,空则正确都匹配,不空则证明有没匹配的。
代码中采用字典方式,将右括号作为键,将左括号作为值。
def isValid(self, s): """ :type s: str :rtype: bool """ stack = [] dict = {')':'(', ']':'[', '}':'{'} for char in s: if char in dict.values(): stack.append(char) elif char in dict.keys(): if stack==[] or stack.pop()!=dict[char]: return False else: return False return stack==[]
阅读全文
0 0
- Leetcode-Valid Parentheses-Python
- 【Leetcode】【python】Valid Parentheses
- 【LeetCode with Python】 Valid Parentheses
- 20. Valid Parentheses Leetcode Python
- leetcode 020 Valid Parentheses(Python)
- [leetcode:python]20.Valid Parentheses
- 【Leetcode】【python】Longest Valid Parentheses
- 【LeetCode 20】Valid Parentheses(Python)
- Python学习——leetcode(Valid Parentheses)
- leetcode 日经贴,python code -Valid Parentheses
- leetcode 日经贴,python code -Longest Valid Parentheses
- [Leetcode]32. Longest Valid Parentheses @python
- 【leetcode】20. Valid Parentheses(Python & C++)
- LeetCode: Valid Parentheses
- LeetCode: Longest Valid Parentheses
- LeetCode: Valid Parentheses
- LeetCode Longest Valid Parentheses
- [Leetcode] Valid Parentheses
- C++的运算符重载
- Krpano插件---view标签说明
- 学习(Broadcast Receiver) 个人笔记三
- Android 项目导入错误
- 浅谈MVP的使用
- Leetcode-Valid Parentheses-Python
- Tyvj 小Y的问题
- 美团—大众软件测试开发校招题
- hdu1823二维线段树,单点跟新,区间求最大值。
- Android_性能优化ViewPager加载高清大图oom解决方案
- 检查粘贴的内容
- StringBuffer与String的区别与应用
- Machine learning11降维与度量学习(二)-------PCA
- 如何改善PDF Transformer+中的识别语言