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

这个题目就用stack, 不能用count了, 因为{[}]是invalid, 但是count没法判断. 那么为了elegant, 建一个hash table, d = {']':'[', '}':'{', ')':'('}, 如果在d里面, 就通过判断stack是否为空和stack.pop() 是否跟d[c] 相等,

如果在d.values()里面, 就append进入stack.

Code:  T: O(n)  S; O(n)

class Solution:
def validParenthesis(self, s):
stack, d = [], {']':'[', '}':'{', ')':'('}
for c in s:
if c in d and (not stack or stack.pop() != d[c]):
return False
elif c in d.values():
stack.append(c)
return not stack

[LeetCode] 20. Valid Parentheses_Easy tag: Stack的更多相关文章

  1. [Leetcode] 20. Valid Parentheses(Stack)

    括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...

  2. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  3. Leetcode 20 Valid Parentheses stack的应用

    判断括号是否合法 1.用stack存入左括号“([{”,遇到相应的右括号“)]}”就退栈 2.判断stack是否为空,可以判断是否合法 class Solution { public: bool is ...

  4. [LeetCode] 20. Valid Parentheses 验证括号

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

  5. [LeetCode] 20. Valid Parentheses ☆

    转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...

  6. [LeetCode] 394. Decode String_Medium tag: stack 666

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  7. [LeetCode] 20. Valid Parentheses 合法括号

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

  8. LeetCode 20 -- Valid Parentheses

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

  9. 20. Valid Parentheses(stack)

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

随机推荐

  1. jquery-1.11.2.min.js

    /*! jQuery v1.11.2 | (c) 2005, 2014 jQuery Foundation, Inc. | jquery.org/license */ !function(a,b){& ...

  2. MyEclipse中同时启动两个tomcat

    开发的时候,有些时候需要同时启动两个项目.首先配置tomcat,方法如下:(转载自:http://bendan123812.iteye.com/blog/1716789) 一.把Tomcat复制一份并 ...

  3. [No0000F9]C# 运算符重载

    您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其他函数一样,重载运 ...

  4. cordova 内部API 用ssl https,报错

    环境:node6.10.1 cordova 6.x, ionic 2.2.1 用cordova/ionic 建立的app我们的api 地址要用https,做了安全加密之后,按照正常的流程,打包,然后跑 ...

  5. 数学用语中的 透明 transitive property

    1.透明 https://en.wikipedia.org/wiki/Equivalence_relation In mathematics, an equivalence relation is a ...

  6. Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案

    使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题). 若要转换成DateTime没有T,可以加上特性: pub ...

  7. io.UnsupportedOperation: not readable

    两处错误一.你是用open打开一个文件,此时调用的是w写入模式,下面使用read是没有权限的,你得使用w+读写模式二.使用write写入一个字符s,但是此时并没有真正的写入,而是还存在与内存中.此时执 ...

  8. Windbg:通过vmware调试进行内核调试

    虚拟机环境搭建 参考:http://www.douban.com/note/416264043/ windbg+vmware+win7 内核 http://blog.csdn.net/G_Spider ...

  9. 【PyQt5-Qt Designer】QSlider滑块

    QSlider滑块 QSlider简介 QSlider小部件提供了一个垂直或水平滑块. 滑块是控制有界值的经典控件.它允许用户沿水平或垂直凹槽移动滑块手柄,并将手柄的位置转换为合法范围内的整数值. Q ...

  10. Javabean非空变量校验工具

    摘要 调研java Class getDeclaredFields() 与getFields()的区别,设计请求参数为Javabean时,基于反射机制校验其中非空变量的公共方法. java Class ...