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. B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...

  2. python 中 try ...except

    捕捉异常 try: 下的代码段 即为 需要捕捉异常的代码段: except:  捕获某一模块的异常,须带异常模块名称,可带原因参数:except 下代码为该异常发生时,所执行的代码:一个try可对应多 ...

  3. 老师的blog整理 .网络编程部分 .网络编程部分 前端部分 django基础部分

    老师的blog整理 python基础部分: 宝哥blog: https://www.cnblogs.com/guobaoyuan/ 开哥blog: https://home.cnblogs.com/u ...

  4. [No0000179]改善C#程序的建议2:C#中dynamic的正确用法

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...

  5. 2014年蓝桥杯省赛A组c++第3题(数组构造+暴力求解)

    /* 标题:神奇算式 由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成. 比如: 210 x 6 = 1260 8 x 473 = 3784 27 x 81 = 2187 都符合要 ...

  6. [未解决:快速滑动collectionveiw请求数据崩溃]:unable to allocate 6553600 bytes for bitmap data

    崩溃:unable to allocate 6553600 bytes for bitmap data

  7. LeetCode 908 Smallest Range I 解题报告

    题目要求 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...

  8. jquery validate强大的jquery表单验证插件

    jquery validate的官方演示和文档地址: 官方网站:http://jqueryvalidation.org/ 官方演示:http://jqueryvalidation.org/files/ ...

  9. 《Redis 集群》

    由于集群这章节内容较多,也比较重要,所以单独拉出来,做一个小章节. 1:如何搭建一个集群? - 环境为 Ubuntu16.04 - 这里我预计使用 9001 - 9006 端口,生成一个 6 台机器的 ...

  10. java web如何获取客户端的请求ip

    String ipAddress = request.getHeader("X-FORWARDED-FOR");if (ipAddress == null) { ipAddress ...