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. bzoj 4199: [Noi2015]品酒大会【后缀数组+单调栈+并查集】

    用SA求出height数组,然后发现每个height值都有一个贡献区间(因为点对之间要依次取min) 用单调栈处理出区间,第一问就做完了 然后用并查集维护每个点的贡献(?),从大到小枚举height, ...

  2. TensorFlow多线程输入数据处理框架(四)——输入数据处理框架

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 输入数据处理的整个流程. #!/usr/bin/env python # -*- coding: UTF-8 -* ...

  3. 定位,标记,Socket通信传输位置

    # -*- coding: utf- -*- """ Editor : X-POWER Date : -- Motto : talk is cheap,show me y ...

  4. hive的使用 + hive的常用语法

    本博文的主要内容有: .hive的常用语法 .内部表 .外部表 .内部表,被drop掉,会发生什么? .外部表,被drop掉,会发生什么? .内部表和外部表的,保存的路径在哪? .用于创建一些临时表存 ...

  5. BZOJ5484(LIS性质+树状数组)

    题目传送 学习的这篇题解. 结论: 1.直观感受一下会发现找到LIS,LIS里的东西相对位置是不会变的,其他的移一移总会排序成功的,所以其他的就是最小集合了,第一问的答案就是n-LIS: 2.寻找字典 ...

  6. 寻找项目中顶级Vue对象 (一)

    个人博客首发博客园: http://www.cnblogs.com/zhangrunhao/ 参考 感谢作者 从一个奇怪的错误出发理解 Vue 基本概念 安装 - Vue.js 渲染函数 - Vue. ...

  7. adb logcat教程

    1.速查 $adb logcat -g //打印和缓冲区使用情况 $adb logcat -c main //清除main缓存区 $adb logcat -v threadtime -f /data/ ...

  8. Problem D: 勤奋的涟漪2 dp + 求导

    http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3 dp[i][state]表示处理了前i个,然后当前状态是state的时候的 ...

  9. Oracle历史版本及oracle相关软件下载地址

    网站:https://edelivery.oracle.com/ 可能需要注册个账号!!!(账号注册登录自己折腾下就好了) 下载数据库或者oracle的相关软件的话,如下 选择对应的下载即可!

  10. SonarQube+Svn+Jenkins环境搭建----问题总结

    1.配置SVN后提示unable to access to repository,原因是使用的账户没有访问svn的权限,创建新的用户即可.注意新的用户,用户名,密码要跟svn上的权限一致.     创 ...