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. Python装饰器探险

    关于python装饰器的理解和用法,推荐廖雪峰老师和这一篇博客以及知乎 以下代码均已手动敲过,看完本篇内容,包你装饰器小成! 装饰器实际上就是为了给某程序增添功能,但该程序已经上线或已经被使用,那么就 ...

  2. ad 层次绘图遇到的元件堆积问题

    元器件复用一般我们使用 reapeat 来复用 总线形式引出各个引脚,有时候我们也可以通过简单的复制实现.但是注意上图 原理图作为一个元件使用,他和单个元件一样必须有唯一ID,名字,不然也会出现冲突, ...

  3. ASP.NET MVC 母版页

    为什么使用母版页?为了整个站点样式统一,任何WEB应用程序都应该使用母版页.MVC框架中,有新的方式为母版页传递数据.       一个WEB应用程序可以包含多个母版页,母版页用于定义页面布局,它与普 ...

  4. “对外部(局部)变量的访问”是C语言函数指针的最大弱点

    1.“对外部(局部)变量的访问”是C语言函数指针的最大弱点 . #include <stdio.h> #include <stdlib.h> /* 结构体定义 */ struc ...

  5. ssm,dubbo框架搭建得配置文件

    在建立了parent,common,manager(pojo,mapper,sercie,web)的maven工程后,开始导入添加配置文件: pojo,mapper 最终会打成jar包,service ...

  6. c#构造函数对string类型赋初值

    public class Stu { public Stu() { //当成员属性非常多难以一一赋值时,采用本方法.当然写代码逐一成员直接赋值效率更高. AssignEmptyStringMember ...

  7. (转载)centos7启用端口

    转载:原文地址:https://www.cnblogs.com/moxiaoan/p/5683743.html   1.firewalld的基本使用 启动: systemctl start firew ...

  8. java 线程(四)线程安全 同步方法

    package cn.sasa.demo2; import java.util.concurrent.ExecutionException; public class ThreadDemo { pub ...

  9. 分布式文档系统_document查询内部原理

    1.客户端发送请求到任意一个node,成为coordinate node2.coordinate node对document进行路由,将请求转发到对应的node,此时会使用round-robin随机轮 ...

  10. UIView和layer的区别

    每个 UIView 内部都有一个 CALayer 在背后提供内容的绘制和显示,并且 UIView 的尺寸样式都由内部的 Layer 所提供.两者都有树状层级结构,layer 内部有 SubLayers ...