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. 下一个元素是左边的,直接入栈;2.下一个元素是右边的,要看能否和栈顶元素匹配,不匹配当然有问题了;3.匹配的话,刚好抵消掉栈顶元素,栈顶元素出栈;最后,判断下栈有没有完全抵消即可。

# -*-coding:utf-8-*-
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
left = ['(','[','{']
right = [')',']','}']
l = list(s)
stack = []
left_count,right_count = 0,0
#从左到右遍历字符串
while len(l)>0:
#如果这个字符是在left中,就写进stack
if l[0] in left:
stack.append(l[0])
#如果这个字符在right中,先判断stack,如果stack为空,直接返回False,如果stack的最后一位和该字符不匹配(否则会出现交叉情况,例如([)]),也直接返回False
#如果stack的最后一位和该字符刚好匹配,则从stack中弹出匹配到的字符
elif l[0] in right:
if len(stack)==0:return False
#print (l[0],' ',stack[-1])
#print (right.index(')'))
if right.index(l[0])==left.index(stack[-1]):
stack.pop()
else:
return False
#如果输入不是这些,也返回False,题目中说了 just the characters,只有这六个字符
else:
return False
l.remove(l[0])
#最后判断stack是否刚好抵消
return len(stack)==0 if __name__=='__main__':
s = '(('
a = Solution()
print (a.isValid(s))

20. Valid Parentheses的更多相关文章

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

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

  2. 20. Valid Parentheses【leetcode】

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

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

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

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

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

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

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

  6. leetCode练题——20. Valid Parentheses

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

  7. 刷题20. Valid Parentheses

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

  8. C# 写 LeetCode easy #20 Valid Parentheses

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

  9. [LeetCode] 20. Valid Parentheses 验证括号

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

  10. [leetcode]20. Valid Parentheses有效括号序列

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

随机推荐

  1. 关于SharpDevelop 4版本以上没有ILAsm模板项目问题

    学习CIL的时候遇到的问题,记录一下. 查了一下英文网站,SharpDevelop 3 版本有ILAsm模板,但是有人运行3版本的CIL项目有问题. 说明一下SharpDevelop 4版本添加ILA ...

  2. OBS MAC 系统开发(基于mac OS X 10.12)

    按照github 上的说明,安装配套软件,和跟踪需要的库 推荐使用homebrew 来安装各种依赖库. 安装Qt后,要配置系统变量 ,这个困扰本人很久:) 成功编译 cmake .. &&am ...

  3. Nginx 1.10.1 编译、配置文档(支持http_v2,TLSv1.2,openssl v1.0.2)

    1.安装常用工具及基础包: [root@localhost /]# yum -y install wget git vim make gcc gcc-c++ openssl-devel [root@l ...

  4. Theano printing

    Theano printing To visualize the internal relation graph of theano variables. Installing conda insta ...

  5. 关于ACM的总结

    看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ...

  6. Le lié à la légèreté semblait être et donc plus simple

    Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...

  7. Forward+

    http://aras-p.info/blog/2012/03/02/2012-theory-for-forward-rendering/ http://www.slideshare.net/taka ...

  8. Struts2 更改校验配置文件位置

    @(Java)[Struts|Interceptor] Struts2 更改校验配置文件位置 在Struts2中提供的拦截器校验ValidationInterceptor,该校验器中默认的配置文件位于 ...

  9. Android之卫星菜单的实现

    卫星菜单是现在一个非常受欢迎的“控件”,很多Android程序员都趋之若鹜,预览如下图.传统的卫星菜单是用Animation实现的,需要大量的代码,而且算法极多,一不小心就要通宵Debug.本帖贴出用 ...

  10. linux查看端口占用情况

    今天要使用python写一个端口探测的小程序,以检测一些特定的服务端口有没有被占用,突然发现自己居然不知道在linux中如何查询端口被占用的情况,天呐,赶快学习一下.