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 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
随机推荐
- android之location01
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <Linear ...
- 查看MySQL的错误日志的方法
我们经常在运行MySQL时会出一些错误,也经常被这些错误搞得晕头转向.当然解决这些问题的首要任务是找到日志信息. MySQL的错误信息是在data目录下的,且文件名为<hostname>. ...
- Liferay 6开发学习(二十六):数据库连接相关问题
Liferay中怎么更换数据库? 常碰到有人问Liferay怎么更换数据库,怎么修改数据库连接.在我们第一次启动Liferay的时候,会有一个配置向导,在此配置向导我们可以选择数据库,并配置数据库连接 ...
- DXperience-11.1.5 破解
将DXPerience_11.1.5_Crack里的所有文件粘贴到DXperience-11.1.5的bin文件夹下,然后在cmd运行register.bin
- Delphi进制转换(二进制/十进制/十六进制)
http://www.cnblogs.com/ywangzi/archive/2012/12/12/2815219.html Delphi进制转换(二进制/十进制/十六进制) 2009-11-2 ...
- 【Oracle经典】132个oracle热门精品资料——下载目录
电子书为网友wglzaj精心整理,这批资料下载量好评率都非常高,广受oracle学习者欢迎.文档共整理了12个精品专题和120个热门资料的下载地址,推荐给大家希望大家喜欢. 目录0豆下载地址:http ...
- [HDU 5074] Hatsune Miku (动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5074 题目大意是给你m个note,n个数,得分是v[a[i]][a[i+1]]的总和,如果说a[i]是 ...
- MS文件与路径的一些方法记录
一,路径相关 String path1 = System.IO.Directory.GetCurrentDirectory(); MessageBox.Show("获取应用程序的当前工作目录 ...
- AIX配置时间服务器(NTP)
xntpd是关于网络时间协议的守护进程,它遵循了因特网时间服务器的通用标准.在启动 xntpd 时, xntpd 会读取 /etc/ntp.conf 配置文件来确定网络中系统时钟服务器,以 ntp 服 ...
- [转] iOS开发之使用lipo命令制作模拟器与真机通用静态库
转自 http://blog.csdn.net/jinglijun/article/details/8276089 通常在项目中使用静态库的时候都会有两个版本,一个用于模拟器,一个用于真机,因为Mac ...