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. 01List.ashx(班级列表动态页面)

    01List.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...

  2. 使用python爬虫爬取股票数据

    前言: 编写一个爬虫脚本,用于爬取东方财富网的上海股票代码,并通过爬取百度股票的单个股票数据,将所有上海股票数据爬取下来并保存到本地文件中 系统环境: 64位win10系统,64位python3.6, ...

  3. ThinkPHP框架 系统规定的方法查询数据库内容!!同时也支持原生的SQL语句!

    <?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller{ ...

  4. 在windows下搭建vueJS开发环境

    转自:https://www.cnblogs.com/RexSheng/p/6934413.html nodejs官网http://nodejs.cn/下载安装包,无特殊要求可本地傻瓜式安装,这里选择 ...

  5. day2 二、编程语言、python解释器和变量

    一.编程语言分类 1.机器语言 直接用计算机能理解的二进制指令编写程序,直接控制硬件,需要了解硬件的操作细节. 2.汇编语言 用英文标签取代二进制编写程序,也是直接控制硬件,也需要了解硬件的操作细节. ...

  6. vue项目打包后一片空白及资源引入的路径报错解决办法

    网上很多说自己的VUE项目通过Webpack打包生成的list文件,放到HBulider打包后,通过手机打开一片空白.这个主要原因是路径的问题. 1.记得改一下config下面的index.js中bu ...

  7. content-type对照表

  8. .NET Core开发日志——ADO.NET与SQL Server

    在.NET世界,如果想要对数据库进行操作,总少不了ADO.NET的身影.在.NET Core里同样离不开那些熟悉的类库与API.这里简略地介绍下如何通过ADO.NET对SQL Server进行不同的处 ...

  9. [No0000139]轻量级文本编辑器,Notepad最佳替代品:Notepad++

    在详细介绍Notepad++之前,先来解释一下,为何要选择Notepad++,即把常见的一些文本编辑器和Notepad++比较,看看其有哪点好: 常见的文本编辑器有很多,此处,只提及Notepad,N ...

  10. 部署Java项目到阿里云服务器主机

    https://m.aliyun.com/jiaocheng/548684.html https://blog.csdn.net/qq_30865575/article/details/7827329 ...