20. Valid Parentheses(括号匹配,用桟)
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.
class Solution:
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
d={'(':')','[':']','{':'}'}
for i in s:
if i in d.keys():#遇到左括号 压桟
stack.append(i)
else: #遇到右括号
if stack ==[]: #桟为空,没有匹配的
return False
else:
if i==d[stack[-1]] : #如果匹配上,弹桟
stack.pop()
else:
return False #没有匹配上
return stack==[] #如果遍历完之后桟为空,则全部匹配
20. Valid Parentheses(括号匹配,用桟)的更多相关文章
- 20. Valid Parentheses - 括号匹配验证
Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- [转]Loadrunner Error code 10053 & Tomcat 连接器(connector)优化
LoadRunner提示错误:Error : socket0 - Software caused connection abort. Error code : 10053. 在今天的测试过程中发现,s ...
- javascript new Date()函数在不同浏览器上返回不同的值
今天是2015年3月16日,在IE 6 上new Date()返回给后台server的字符串格式为 "Mon Mar 16 00:00:00 UTC 0800 2015",而在f ...
- Cognos组织架构介绍
Cognos只是一个工具,说到Cognos相信大部分人都知道BI(商业智能,Business Intelligence). Cognos也是属于SOA架构,面向服务的体系结构,是一个组件模型,它将应用 ...
- 解决多指操作放大缩小 指针错误 java.lang.IllegalArgumentException: pointerIndex out of range
/** Custom your own ViewPager to extends support ViewPager. java source: */ /** Created by azi on 20 ...
- FZU 2140 Forever 0.5(找规律,几何)
Problem 2140 Forever 0.5 Accept: 371 Submit: 1307 Special Judge Time Limit: 1000 mSec Memory Limit : ...
- poj1179 Polygon【区间DP】
Polygon Time Limit: 1000MS Memory Limit: 10000K Total Submissions:6633 Accepted: 2834 Descriptio ...
- mysql状态分析之show global status
这里整理下mysql global status的相关命令,在计算监控数据的时候需要用到 一.慢查询 show variables like '%slow%'; +------------------ ...
- 【Python Programe】WSGI (Web Server Gateway Interface)
Part1: What is a Web server? 一个位于物理服务器上的网络服务器(服务器里的服务器),等待客户端去发送request,当服务器接收到request,就会生成一个respons ...
- 微软构建高效DevOps团队培训总结
9.21和9.22这两天参加了微软DevOps的培训,主要是围绕TFS2015的不少新功能来讲的,相比较之前我们一直使用TFS2013来管理团队,确实强大了不少,也更加实用了. 首先,什么是DevOp ...
- EntityFramework.DynamicFilters 实现软删除和租户过滤
EntityFramework.DynamicFilters 实现软删除和租户过滤