LeetCode算法题——20. Valid Parentheses
来源:互联网 发布:大连美工培训班哪里好 编辑:程序博客网 时间:2024/06/13 17:55
题目
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.采用栈机制,对于每个进栈元素都与栈顶元素进行匹配,若匹配成功,这栈顶元素弹出,若匹配不成功,则进栈
Pyhon实现:
# -*- coding:utf-8 -*-class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ brackets=[] for i in range(0,s.__len__()): pipei=False if len(brackets)!=0: b=brackets.pop() if b=='(': if s[i]==')': pipei=True elif b=='{': if s[i]=='}': pipei=True elif b=='[': if s[i]==']': pipei=True if pipei==False: brackets.append(b) brackets.append(s[i]); else: brackets.append(s[i]) if len(brackets)==0: return True else: return False if __name__=="__main__": s=Solution() res=s.isValid("{[)(]}") print(res)
0 0
- LeetCode算法题——20. Valid Parentheses
- LeetCode——20. Valid Parentheses
- Leetcode——20. Valid Parentheses
- LeetCode——Valid Parentheses
- 【leetcode】20—valid parentheses
- python写算法题:leetcode: 20. Valid Parentheses
- LeetCode算法题之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
- [leetcode] 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- 20. Valid Parentheses LeetCode
- Buy Tickets
- I Hate It 【线段树】
- 洛谷 P2831 愤怒的小鸟
- 事件库之Libev(一)
- 装备选择
- LeetCode算法题——20. Valid Parentheses
- PDSOJ 1048 Fibonacci Numbers(大数)
- 粗谈自定义控件
- 线段树更新单个节点hdu 1166 敌兵布阵
- javascript基础
- Windows消息映射及消息发送(SendMess、PostMess)实现
- 我就是我
- JPA常用注解
- Spring框架学习(8):通过工厂方法配置bean