Easy-题目47:20. Valid Parentheses
来源:互联网 发布:mac如何卸载opera 编辑:程序博客网 时间:2024/06/05 20:24
题目原文:
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.
题目大意:
给出一个括号字符串,判断括号是否正确匹配。
题目分析:
本题考查堆栈的使用,遇到左边的括号全部入栈,遇到右边的括号则看栈顶的括号是不是对应的左括号,如是则弹出,如不是则返回错误。如果整个括号字符串扫完之后栈恰好为空,则是合法匹配。
源码:(language:python)
class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ stack=[] for c in s: if c in ['(','[','{']: stack.append(c) else: if(len(stack)==0): return False top=stack[len(stack)-1] if ((c == ')' and top=='(') or (c == ']' and top=='[') or (c == '}' and top == '{')): stack.pop() else: return False return len(stack)==0
成绩:
43ms,beats 55.57%,众数40ms,27.91%
0 0
- Easy-题目47:20. Valid Parentheses
- 20. Valid Parentheses (Easy)
- 20. Valid Parentheses [easy] (Python)
- 20.Valid Parentheses(Stack-Easy)
- Leetcode 20. Valid Parentheses (Easy) (cpp)
- 【leetcode】Valid Parentheses(easy)
- Valid Parentheses (EASY)
- [Leetcode 20, Easy] Valid Parentheses
- 【Leetcode-Easy-20】 Valid Parentheses
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
- LeetCode-Easy部分中标签为String 20. Valid Parentheses
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
- LeetCode-20-Valid Parentheses(堆栈)-Easy
- Easy 5 Valid Parentheses(20)
- LeetCode OJ 20 Valid Parentheses [Easy]
- LeetCode-Easy刷题(5) Valid Parentheses
- Leetcode 题目 20. Valid Parentheses(个人错误分析)
- LeetCode 20. Valid Parentheses
- 鼠标经过旋转特效
- 神经网络浅析
- 【HDU】 1431 素数回文
- new一个对象的过程
- Package.json中dependencies依赖包中^符号和~符号前缀的区别
- Easy-题目47:20. Valid Parentheses
- 百度地图api
- VS2015找不到stdlib.h的问题
- Spring.No1 ----整理---Spring学习的第一课
- 多模匹配算法之Aho-Corasick
- shell(三)
- 如何创建一条可靠的实时数据流
- Dhroid框架六大组件之视图注入
- Easy-题目48:299. Bulls and Cows