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. 浅谈servlet

    刚开始接触servlet的时候,其实不是太理解servlet的,后来经过慢慢摸爬滚打式的的学习,有了一点自己的理解. servlet的产生还要从Java和HTTP说起: Java的servletAPI ...

  2. Eclipse安装scala

    Scala官方提供了三种插件,分别支持Eclipse.NetBeans和Intellij IDEA开发环境.要在Eclipse IDE下安装Scala插件:选择Eclipse的菜单项Help--> ...

  3. Nike Zoom Winflo 2 Kvinder Sko Når jeg set elementet

    De fleste af os elskede denne Nike Pegasus 34 foruden var ved at blive begejstret for at få dine ben ...

  4. XPath 学习二: 语法

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) 来选取的. 下面列出了最有用的路径表达式: 表达式 描述 nodename 选 ...

  5. myeclipse竖行删除

    1.Alt + shift + a   

  6. 分享公司DAO层数据库结果映射到对象的方法

    主题 前面写过一篇文章,分享了公司是怎么动态封装SQL查询条件的(http://www.cnblogs.com/abcwt112/p/5874401.html). 里面提到数据库查询结果二维数组最后是 ...

  7. ThinkPHP 隐藏URL中的 index.php

    去掉 URL 中的 index.php 通常的URL里面含有index.php,为了达到更好的SEO效果可能需要去掉URL里面的index.php ,通过URL重写的方式可以达到这种效果,通常需要服务 ...

  8. php数组操作集锦- 掌握了数组操作, 也就掌握了php

    参考下面的文章, 是很好的: http://www.cnblogs.com/staven/p/5142515.html http://pcwanli.blog.163.com/blog/static/ ...

  9. 锁(MySQL篇)—之MyISAM表锁

    前言 锁是计算机协调多个进程或线程并发访问某一资源的机制,在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是 ...

  10. 关于java的格式输出控制

    java中的System.out.println()功能十分强大,可以对任意类型的数据进行输出. 这里我们不讨论System.out.println(),而是讨论System.out.printf() ...