# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheses
https://oj.leetcode.com/problems/valid-parentheses/ 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. ===Comments by Dabay===
用一个stack来保存左括号,如果来的是对应的右括号,就pop一个;如果来的是不对应的右括号,直接return False。
''' class Solution:
# @return a boolean
def isValid(self, s):
stack = []
for c in s:
if c in "([{":
stack.append(c)
else:
if len(stack) == 0:
return False
if (
(c == ")" and stack[-1] == "(") or
(c == "]" and stack[-1] == "[") or
(c == "}" and stack[-1] == "{")
):
stack.pop()
else:
return False
else:
return len(stack) == 0 def main():
sol = Solution()
print sol.isValid("()[]{}") if __name__ == '__main__':
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]20: Valid Parentheses的更多相关文章

  1. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. C# 写 LeetCode easy #20 Valid Parentheses

    20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  3. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  4. 【一天一道LeetCode】#20. Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  5. LeetCode题解(20)--Valid Parentheses

    https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...

  6. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  7. 【LeetCode】20. Valid Parentheses

    题目:

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

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

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

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

随机推荐

  1. Jsoup小Demo

    Jsoup小Demo public class JsoupUtil { public void parseWangYi() { Document doc = null; try { //eg1:解析百 ...

  2. View的事件分发机制

    一.点击事件的传递规则 传递事件的主要方法: public boolean dispatchTouchEvent(MotionEvent ev) 如果事件能够传递到当前View,则该方法一定会被调用. ...

  3. 数独破解c++代码

    数独破解c++代码 #include <iostream> #include <cstring> #include <cstdio> #include <st ...

  4. 算法分析-堆排序 HeapSort 优先级队列

    堆排序的是集合了插入排序的单数组操作,又有归并排序的时间复杂度,完美的结合了2者的优点. 堆的定义 n个元素的序列{k1,k2,…,kn}当且仅当满足下列关系之一时,称之为堆. 情形1:ki < ...

  5. Android02-Activity01

    1.概念:活动是一种可以包含用户界面的组件, 主要用于和用户进行交互. 2.常见操作:      1.隐藏Activity的标题栏: @Override protected void onCreate ...

  6. 转: JavaScript函数式编程(二)

    转: JavaScript函数式编程(二) 作者: Stark伟 上一篇文章里我们提到了纯函数的概念,所谓的纯函数就是,对于相同的输入,永远会得到相同的输出,而且没有任何可观察的副作用,也不依赖外部环 ...

  7. KEIL MDK环境下uCOS-II在LPC17xx上的移植实例

    1. 知识准备 要想对ucos-ii的移植有较深的理解,需要两方面知识: (1)目标芯片,这里是lpc17xx系列芯片,它们都是基于ARMv7 Cortex-M3内核,所以这一类芯片的ucos-ii移 ...

  8. Qt部件--烧肉

    1,QSplitter

  9. aix vg lv pv

    lsvg lsvg -o lsvg rootvg 查看rootvg的信息 lsvg -p rootvg 查看rootvg卷里的物理硬盘以及分布信息 lsvg -l rootvg 查看rootvg卷下的 ...

  10. 利用PowerDesigner15在win7系统下对MySQL 进行反向project(二)

    利用PowerDesigner15在win7系统下对MySQL 进行反向project 1.打开PowerDesigner,建立新模型.选择Physical Data Model中的Physical ...