Valid Parentheses [LeetCode 20]
1- 问题描述
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.
2- 思路分析[1]
利用一个栈来保存前括号,然后有后括号来时弹出栈顶来判断。
3- Python实现
class Solution:
# @param {string} s
# @return {boolean}
def isValid(self, s):
if not s: return False
l = len(s)
if l % 2 == 1: return False # 长度为奇数返回False
bracket = {'(':')', '[':']', '{':'}'}
stack = [] # 栈保存前括号
for i in range(l):
if s[i] in bra:
stack.append(s[i]) # 前括号则入栈
else:
try:
top = stack.pop()
except:
return False # 取不出前括号返回False
if bracket[top] != s[i]: # 比较是否与出栈前括号匹配
return False
if not len(stack): return True # 遍历完若栈为空,则完全匹配
return False # 栈内还有元素,则不匹配
[1] [LeetCode] Valid Parentheses
Valid Parentheses [LeetCode 20]的更多相关文章
- Valid Parentheses - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Longest Valid Parentheses - LeetCode
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Valid Parentheses leetcode java
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
随机推荐
- Mac下安装JDK 6
https://support.apple.com/kb/DL1572?viewlocale=zh_CN&locale=en_US 下载 , 安装
- sed 使用 删除匹配行
“p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its ...
- Java将一段逗号分割的字符串转换成一个数组
String 类:String 类代表字符串.Java 程序中的所有字符串字面值都作为此类的实例实现.字符串是常量,它们的值在创建之后不能更改.字符串缓冲区支持可变的字符串.因为 String 对象是 ...
- context.Request.Files为NULL问题
在实现图片上传功能的时候出现在ashx等处理页面出现context.Request.Files为NULL异常,有几点需要注意: 1.在客户端可以将form用submit提交,如下: <%@ Pa ...
- 一些代码 I (斐波那契、for...else...、try和return、classmethod、统计个数)
1. 斐波那契 from itertools import islice def fib(): a, b = 0, 1 while True: yield a a, b = b, a+b print ...
- [SQL]不知道
--COALESCE --返回其参数中第一个非空表达式. --语法 --COALESCE ( expression [ ,...n ] ) --参数 --expression --任何类型的表达式. ...
- Python子类方法的调用(类方法)
class S(object): def Test(self): print("TEST") @classmethod def Test02(cls): print("c ...
- spi驱动无法建立spidev问题
参考这里: http://e2e.ti.com/support/arm/sitara_arm/f/791/t/168122.aspx http://communistcode.co.uk/blog/b ...
- 日期:Date
API--- java.util.Date:日期类,月份从0-11: 日期对象和毫秒值之间的转换. 1,日期对象转成毫秒值.Date类中的getTime方法. 2,如何将获取到的毫秒值转成具体的日期呢 ...
- UltraEdit 除去行首的行号和空格
我们在复制代码的时候,经常会发生这种事情. 例如:如下文件(lpc17xx_libcfg.h) 00001 /********************************************* ...