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.

Example 1:                 Input: "()"              Output: true

Example 2:                Input: "()[]{}"          Output: true

Example 3:                Input: "(]"              Output: false

Example 4:                Input: "([)]"            Output: false

Example 5:                Input: "{[]}"            Output: true

思路


  我们可以设置一个辅助空间栈来保存括弧的左半边,然后进行遍历。当遍历到不是右半边括弧时,我们就弹出最上面的括弧来和当前括弧进行对比,查看是否是有效括弧。 时间复杂度为O(n)(n为字符串的长度), 空间复杂度为O(n)。

图示思路


   

   

     一直到遍历完毕

代码


 class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s) < : # 空字符串直接返回
return True
tem_dict = {')':'(', '}':'{', ']':'['} # 设置辅助字典
stack = [] # 设置辅助栈
for i in s: # 进行遍历
if i not in tem_dict:
stack.append(i)
else:
if stack: # 右括弧进行选择
t = stack.pop()
if t != tem_dict[i]:
return False
else:
return False
if stack: # 栈不为空的话返回False
return False
return True

【LeetCode每天一题】Valid Parentheses(有效的括弧)的更多相关文章

  1. leetcode第20题--Valid Parentheses

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

  2. LeetCode解题报告—— Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

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

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

  4. [Leetcode][Python]32: Longest Valid Parentheses

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

  5. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  6. LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses

    1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...

  7. 【LeetCode算法-20】Valid Parentheses

    LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...

  8. 【LeetCode】32. Longest Valid Parentheses (2 solutions)

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  9. leetcode problem 32 -- Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  10. 【LeetCode练习题】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

随机推荐

  1. 高斯混合模型 GMM

    本文将涉及到用 EM 算法来求解 GMM 模型,文中会涉及几个统计学的概念,这里先罗列出来: 方差:用来描述数据的离散或波动程度. \[var(X) =  \frac{\sum_{i=1}^N( X_ ...

  2. 安装 powerline

    使用说明: https://powerline.readthedocs.io/en/latest/usage.html ~ vim,在 .vimrc 中添加配置 set rtp+=/usr/lib/p ...

  3. Linux----面试

    1:tcp和udp的区别 TCP:是面向连接的流传输控制协议,具有高可靠性,确保传输数据的正确性,有验证重发机制,因此不会出现丢失或乱序. UDP:是无连接的数据报服务,不对数据报进行检查与修改,无须 ...

  4. docker swarm:执行 service update 过程中服务短暂不能访问的问题

    这是我们使用自建 docker swarm 集群后在部署时遇到的一个问题,使用 docker service update 命令更新服务时, docker service update -d=fals ...

  5. C和C指针小记(四)-浮点类型

    1.浮点型 浮点数家族包括:float,double,long double. ASCII标准规定:long double 至少和 double 一样长,而 double 至少和float 一样长.同 ...

  6. ArcEngine利用索引获取图层

    近期在做GP工具相关的功能,需要获取到图层并用ComboBox列出,比如图层更新: 开始用了根据图层名获取图层,但这样有个弊端,遇到不同文件夹的相同图层名称的图层gg了.本来想利用图层名+路径来区分, ...

  7. C#配置.INI文件

    百度搜了一些资料,好多没给示例,只给了代码.让人看了直接懵逼,后来找了个靠谱的:http://www.jb51.net/article/118591.htm

  8. Android+Tomcat通过http获取本机服务器资源

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  9. [daily][samba] smbclient使用

    用的也不是太明白,反正凑合用吧. 在用之前,只得到了两个信息,1:ip 192.168.30.9.    2:可以免密登录. 1.  用这个命令看一看,主要是找到这个目录:Anonymous ┬─[t ...

  10. 最全的MonkeyRunner自动化测试从入门到精通(1)

    一.环境变量的配置 1.JDK环境变量的配置 步骤一:在官网上面下载jdk,JDK官网网址: http://www.oracle.com/technetwork/java/javase/downloa ...