# -*- 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. Google TensorFlow深度学习笔记

    Google Deep Learning Notes Google 深度学习笔记 由于谷歌机器学习教程更新太慢,所以一边学习Deep Learning教程,经常总结是个好习惯,笔记目录奉上. Gith ...

  2. python学习day2(一)

    一.上周作业回顾 1.登陆接口: 思路流程: 1.登陆,三次锁定用户 2.用户信息文件,黑名单文件 3.检测黑名单,如输入账号在黑名单中存在,不允许登陆 4.用户密码判断 主要知识点:while,fo ...

  3. 【Java 小实验】重写(覆写 Override)返回值类型能不能相同

    背景 每次看到重写那里写着: 重写机制是指子类的方法的方法名.参数表.返回值与父类中被重写的方法都相同,而方法体不同. 而重载是: 方法名与父类中的相同,而参数表不同,则属于同名方法的重载. 本来的感 ...

  4. ASP.net MVC3里的HandleErrorAttribute

    在MVC3里使用HandleErrorAttribte类,有2种方式可以使用它,一是在类或者方法上直接使用HandleError属性来定义: // 在这里声明[HandleError]public c ...

  5. android手机上安装apk时出现解析包错误的一个解决办法

    今天下午在学习安卓开发时,学习开发文档中的gridview时,在模拟器上调试程序一切正常,如下图所示: 但当将bin目录下的HelloGridView.apk拷贝到M8安卓系统后进行安装时,出现了“解 ...

  6. apache重启操作

    方法: apahce启动命令: 推荐/apachectl start apaceh启动 apache停止命令 /apachectl stop   停止 apache重新启动命令: /apachectl ...

  7. 初探ListView和Adapter

    关于Android Adapter(适配器),参考Devin Zhang’s blog.简单的说,Adapter起到的作用是使得前端的显示和后端的数据能够适配,用以下代码作为例子 1234567891 ...

  8. debian安装jdk6

    一般用命令 apt-get install sun-java6-jdk ,会报找不到源的错误. vim /etc/apt/sources.list # 於最下方新增此行 deb http://ftp. ...

  9. AndroidUI 视图动画-透明动画效果 (AlphaAnimation)

    1.新建一个Android项目,Activity添加一个按钮如下代码: <Button android:id="@+id/btnAiphaAnimation" android ...

  10. html表格单元格设置背景颜色