Valid Parentheses
来源:互联网 发布:数控车床螺纹编程 编辑:程序博客网 时间:2024/06/06 16:35
题目要求:输入一个字符串判断括号是否匹配。感觉做简单题,主要考查英语水平[衰]。
题目链接:https://leetcode.com/problems/valid-parentheses/#/description
class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ stack=[] #用list数据类型构造模拟栈 for char in s: if(char=='(' or char=='[' or char=='{'): #左括号统统进栈 stack.append(char) elif char==')' : #遇到右小括号,则需要和比较栈顶符号 if( len(stack)>0 and stack[-1]=='(' ): #若栈非空且是左中括号 stack.pop() else: #其他情况则为括号不匹配的情况 return False elif char==']' : #遇到右中括号,和栈顶符号比较 if( len(stack)>0 and stack[-1]=='[' ): #若栈非空且是左中括号 stack.pop() else: #其他情况则为括号不匹配 return False elif char=='}': if( len(stack)>0 and stack[-1]=='{' ): stack.pop() else: return False if len(stack)==0: #经过循环处理之后,若栈为空,则证明字符串中括号匹配 return True else: #若栈不空,则括号不匹配 return False
阅读全文
0 0
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- java中与数学相关的类
- AngularJs 与Jquery的对比分析,超详细!
- LightOJ
- Apache系列—虚拟主机配置的三种方式(一)
- 在IAR 中出现the stack plug-in failed to set a breakpoint on "main"
- Valid Parentheses
- 初学HTML(二)
- HDU1560 DNA sequence(IDA星) (E)
- Qoo10开店,为什么你不应该担心推广问题,而应该担心销售问题
- Vue实例详解
- Shiro笔记(二)----shiro源码与默认提供的示例
- Color Length UVA
- Universal TUN and TAP device driver
- Apache系列—httpd.conf文件指令详解