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

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
""" if len(s) == 0:
return True dict_ = {'}':'{',']':'[',')':'('} stack = []
for ch in s:
if ch in dict_.values():
stack.append(ch)
elif ch in dict_.keys():
if stack == [] or dict_[ch] != stack.pop():
return False
else:
return False; return len(stack) == 0 以上

20. Valid Parentheses (python版)的更多相关文章

  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. Luogu P1613跑路【倍增】By cellur925

    题目传送门 开始的思路:直接跑一遍最短路,得到最短路的那个值.然后把那个值进行一下二进制拆分,看能拆几次.(可能是受到了刚做过的题影响) ;i>=;i--) { <<i)>qw ...

  2. 同一台服务器上部署多个Tomcat的配置修改方法

    同一服务器部署多个tomcat时,存在端口号冲突的问题,所以需要修改tomcat配置文件server.xml,以tomcat7为例. 首先了解下tomcat的几个主要端口: <Server po ...

  3. UltraEdit - 怎么显示文件标签栏和侧边栏

    显示文件标签栏 view -> views/lists -> open Files Tabs 显示侧边栏 view -> views/lists -> File Tree Vi ...

  4. [在读]javascript框架设计

    司徒正美的书,内容我觉得不错,国内的书很少会讲这些.当然也有很多人吐槽它只贴代码没有解释,文笔不够优美啥啥的,我想说,不要在意这些细节,反正是值得买的一本.

  5. C# 控制台如何播放音频文件

    OK,先看下代码: using System.Reflection; using System.Media; namespace ThePlay { class Program { static vo ...

  6. Masonry自动布局与UIScrolView适配

    Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X.可以通过cocoa ...

  7. 8.2.6 PEB —— PEB结构值不正确的问题

    书中作者使用 dt _PEB xxxxxx 命令来查看当前进程的PEB结构. 实际操作后PEB结构显示的成员值: 作为进程链表的LDR结构居然没有值,这显然是不正常的,地址也没有输错,问题到底出在哪里 ...

  8. Unity3D——Epirome框架_TimerManager计时任务管理器

    1.Timer timer = new Timer(); 创建时间管理器 参数(float time, TimeUnit timeUnit,bool ignoreTimeScale = false, ...

  9. centos下升级python

    1. wget http://www.python.org/ftp/python/3.3.0/Python-3.3.0.tgz 2.解压之后  tar -xzvf Python-3.3.0.tgz 3 ...

  10. jeecms标签

    .@cms_content_list--新闻单页 [@cms_content channelId=' dateFormat='MM-dd' ] [#if tag_list?size>0] < ...