# -*- 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. 自学HTML5第一天(认识HTML5的全局属性)

    contextmenu 属性 规定 <div> 元素的上下文菜单.上下文菜单会在用户右键点击元素时出现.列子: <div contextmenu="mymenu" ...

  2. Selenium2Library使用Remote功能(转载并更新)

    在selenium2library库的open browser中,除了我们常用的url,browser外,还有几个不常用的参数.如:remote_url的用法 1.下载selenium-server- ...

  3. jQuery validate (转载)

    转自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html jQuery校验 官网地址:http://bassistance.de/jquery-p ...

  4. CC++初学者编程教程(12) 基于rhel6.3的Oracle数据库学习环境搭建

    前言 安装oracle 11g系统最好是1G以上内存,硬盘至少需要4.5G空间. 至少环境在Linux Server release 5.3以上. win安装包 win32_11gR2_databas ...

  5. POJ 1182 食物链(并查集拆点)

    [题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y ...

  6. ERROR: modinfo: could not find module rbd FATAL

    CENTOS 6.5 安装CEPH RDB 错误 ERROR: modinfo: could not find module rbd FATAL: Module rbd not found. rbd: ...

  7. The Unique MST (判断是否存在多个最小生成树)

    The Unique MST                                                                        Time Limit: 10 ...

  8. sql server操作2:查询数据库语句大全【转】

    注:以下操作均建立在上篇文章sql Server操作1的数据基础之上 一.实验目的 熟悉SQL语句的基本使用方法,学习如何编写SQL语句来实现查询 二.实验内容和要求 使用SQL查询分析器查询数据,练 ...

  9. 自学XSL的几个例子

    XSL 指扩展样式表语言(EXtensible Stylesheet Language).XSL用来描述XML文件的格式,类似于我们可以用CSS描述HTML的格式.具体用法请转:http://www. ...

  10. JavaScript的日期处理

    很久前写的代码了,偶尔看到贴出来做个备忘,写得有点乱,懒得整理了. // 根据初始日期和滚动周期及滚动次数来计算终止日期,日期滚动周期,// 可以是每天(DAY).每周(WEEK).每月(MONTH) ...