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. Flask web开发之路四

    jinjia2模板 模板渲染和参数传递 项目结构如下: 主app文件代码: from flask import Flask,render_template app = Flask(__name__) ...

  2. 洛谷P1098 字符串的展开【字符串】【模拟】

    题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母或数 ...

  3. robot framework ——关键字run keyword if 如何在一个条件下接多个执行语句,以及如何写复杂条件句

    曾一度疯狂搜索run keyword if 的用法,帖子是挺多的,可惜,没有一个我想要的.现在我终于把我想要的用法,收集好了,在此总结下. 1.曾经天真的以为  run keyword if +条件 ...

  4. grafana----alert

    Alert只有grafana V4.0以上. Introduction(介绍) Grafana中的alert允许在dashboard panels你附加一些规则.当你保存仪表板Grafana将提取的报 ...

  5. Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378 Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378 Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378 Error:..\FreeRTOS\tasks.c,2806

    Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378Error:..\FreeRTOS\portable\RVDS\ARM_CM3\port.c,378 ...

  6. vim保存只读文件时获得sudo权限

    有时使用vim时忘记输入sudo,导致编辑了一大堆文字,最后保存时发现是只读文件,无法保存,每次保存都会提示read only.这时可以使用如下命令代替原有的 :wq 命令 :w !sudo tee ...

  7. Vue SSR 配合Java的Javascript引擎j2v8实现服务端渲染4支持构建bundle

    安装 webpack-node-externals yarn add -D webpack-node-externals

  8. Rabbitmq关于集群节点功能的读书笔记

    消息和队列可以指定是否持久化,如果指定持久化则会保存到硬盘上 ,不然只在内存里 普通集群模式下持久化的队列不能重建了 内存节点和磁盘节点的区别就是将元数据放在了内存还是硬盘,仅此而已,当在集群中声明队 ...

  9. opencv模板匹配有趣的链接

    https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_template_matching/py_template_matchi ...

  10. Java 输入/输出——处理流(ObjectIO)

    Object流:直接将Object流写入或读出. TestObjectIO.java transient关键字(英文名:透明的,可以用来修饰成员变量(实例变量),transient修饰的成员变量(实例 ...