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(括号匹配,用桟)的更多相关文章

  1. 20. Valid Parentheses - 括号匹配验证

    Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...

  2. leetcode 20 Valid Parentheses 括号匹配

    Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...

  3. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  5. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  6. 刷题20. Valid Parentheses

    一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...

  7. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  8. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  9. 20. Valid Parentheses【leetcode】

    20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

随机推荐

  1. RMI几种公布和引用服务的方式

    RMI是Java原生的分布式服务机制.支持Java对Java的分布式訪问.採用Java的序列化协议进行CodeC操作. 这里简单说下RMI公布服务和client引用服务的方式. RMI公布服务时支持两 ...

  2. ios开发之--CGRect/CGSize/CGPoint/CGVector/CGAffineTransform/UIEdgeInsets/UIOffset和NSString之间的转换

    仅做记录,一个函数和字符串之间的互相转换 方法如下: UIKIT_EXTERN NSString *NSStringFromCGPoint(CGPoint point); UIKIT_EXTERN N ...

  3. Spring源码从开始到放弃(一)

    参考<Spring技术内幕>分析. github上面有spring的源码(https://github.com/spring-projects/spring-framework) spri ...

  4. 如何用sql语句复制一张表

    如何用sql语句复制一张表 1.复制表结构及数据到新表 CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete ...

  5. python 学习源

    入门 w3cschool https://www.w3cschool.cn/python/ 菜鸟教程(支持在线编程) http://www.runoob.com/python/python-tutor ...

  6. Eclipse常用快捷键(转载)

    编辑相关快捷键 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户编辑的好帮手 ...

  7. TCL电视直播软件

    升级你的电视系统我的型号46寸 V7300 3D,具体的升级程序在"技术宅"里有下载 找个格式化过的U盘把你的程序拷贝进去,插在电视上,电视会自动升级 当你成功安装V8-0MT32 ...

  8. CentOS中制作本地yum源

    1.光盘指向镜像 2.将镜像挂载到某个目录 mkdir /mnt/cdrom mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom 3.修改本机上的YUM源配置文件 ...

  9. DHCP服务原理

    DHCP 工作原理 一.什么是DHCP? DHCP,动态主机配置协议,前身是BOOTP协议,是一个局域网的网络协议,使用UDP协议工作,常用的2个端口:67(DHCP server),68(DHCP ...

  10. Why did we build Zuul? How We Use Zuul At Netflix

    Home · Netflix/zuul Wiki https://github.com/Netflix/zuul/wiki Arthur Gonigberg edited this page on M ...