1- 问题描述

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

  The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.


2- 思路分析[1]

  利用一个栈来保存前括号,然后有后括号来时弹出栈顶来判断。


3- Python实现

 class Solution:
# @param {string} s
# @return {boolean}
def isValid(self, s):
if not s: return False
l = len(s)
if l % 2 == 1: return False # 长度为奇数返回False
bracket = {'(':')', '[':']', '{':'}'}
stack = [] # 栈保存前括号
for i in range(l):
if s[i] in bra:
stack.append(s[i]) # 前括号则入栈
else:
try:
top = stack.pop()
except:
return False # 取不出前括号返回False
if bracket[top] != s[i]: # 比较是否与出栈前括号匹配
return False
if not len(stack): return True # 遍历完若栈为空,则完全匹配
return False # 栈内还有元素,则不匹配

[1] [LeetCode] Valid Parentheses

Valid Parentheses [LeetCode 20]的更多相关文章

  1. Valid Parentheses - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...

  2. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  3. Longest Valid Parentheses - LeetCode

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. Valid Parentheses leetcode java

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

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

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

  6. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  7. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

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

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

  9. LeetCode 20. 有效的括号(Valid Parentheses)

    20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...

随机推荐

  1. URI

    1, URI (标识.定位任何资源的字符串) 在电脑术语中,统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串. 该种标识允许用 ...

  2. ubuntu安装mysql5.7

    sudo apt-get updatesudo apt-get upgradesudo apt-get install mysql-server mysql-client   #自动安装会装上5.7的 ...

  3. 如何使用javadoc

    package com.frank.chapter1; // object.Documentation1.java // TIJ4 Chapter Object, Exercise 13 - 1 /* ...

  4. Bridge桥接模式

    当我们的功能要在多个维度进行扩展时,各个维度之间可以交叉组合,就可以考虑使用桥接模式. 将抽象部分与实现部分分离,使它们都可以独立的变化.                                ...

  5. htmlparser日记

    myParser = Parser.createParser(response, "utf-8");NodeFilter tableFilter = new NodeClassFi ...

  6. ubuntu13.10下安装samba

    1.现在的ubuntu系统都默认安装了samba服务,如果是精简系统的话,可以通过apt-get进行安装 sudo apt-get install samba sudo apt-get cifs-ut ...

  7. CRM 2016 自动保存 Save event arguments

    Save event arguments (client-side reference)   Applies To: Dynamics CRM 2016, Dynamics CRM Online In ...

  8. [kuangbin带你飞]专题二十 斜率DP

            ID Origin Title   20 / 60 Problem A HDU 3507 Print Article   13 / 19 Problem B HDU 2829 Lawr ...

  9. VS2015+win10+opencv3.0整个安装过程

    LZ最近换了台新台式电脑,开始下载新VS软件,话说软件平台越新越好用,一看网上已经有VS2015版本,果断就去官网下载. 1.安装VS操作 官方网的链接如下:https://www.visualstu ...

  10. 从手工测试转型web自动化测试继而转型成专门做自动化测试的学习路线。

    在开始之前先自学两个工具商业web自动化测试工具请自学QTP:QTP的学习可以跳过,我是跳过了的.开源web自动化测试工具请自学Selenium:我当年是先学watir(耗时1周),再学seleniu ...