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. web.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" ...

  2. (转)关于rdlc报表的数据源

      rdlc 报表字符类数据分为文本数据和表数据,区别就在于文本数据只有一个,表数据可以有多行,然而有很多数据只需要一个传入就可以比如打印某个用户的基本信息,很多信息都是唯一的,如果此时报表传入的数据 ...

  3. flexpaper源码的编译,去除logo和打印 (转)

    1.首先下载FlexPaper的源码.下载地址 2. 下载Adobe Flash Builder v4.5 现在最新是4.6了,在adobe网站木找到4.5下载,这里提供下4.5的种子文件 http: ...

  4. gcc makefile

    $* 不包含扩展名的目标文件名称 $< 第一个依赖文件名称 $? 所有时间戳比目标文件晚的依赖文件 $@ 目标文件完整名称 $^ 所有不重复的依赖文件

  5. Jetty提交数据时报java.lang.IllegalStateException: Form too large270468>200000问题解决

    今天在使用Eclipse的Jetty插件做为服务器提交富文本编辑中的数据时,报如下异常: 在\eclipse\plugins目录下,找到org.mortbay.jetty.server_6.1.23. ...

  6. 你可能不知道的 30 个 Python 语言的特点技巧

        列表按难度排序,常用的语言特征和技巧放在前面. 1.1   分拆 >>> a, b, c = 1, 2, 3>>> a, b, c(1, 2, 3)> ...

  7. poj 3026 Borg Maze 最小生成树 + 广搜

    点击打开链接 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7097   Accepted: 2389 ...

  8. ubuntu14安装ambari2.2

    https://cwiki.apache.org/confluence/display/AMBARI/Install+Ambari+2.2.0+from+Public+Repositories 查看是 ...

  9. SQL语句的执行计划(oracle表的三种链接方式)

    SQL语句我们写完之后,就是分析其优化,这就要求我们了解到底数据是怎么存储. 首先我们需要了解,表链接的几种方式 nested loop join sort merge join hash join ...

  10. android Json 解析和生成

    什么是json: JSON即JavaScript Object Natation的简称,它是一种轻量级的数据交换格式,非常适合服务器与JavaScript的交互.JSON易于人阅读和编写.同时也易于机 ...