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. 标准电流信号为什么是4-20MA?(网络摘录)

    一来源: 4-20mA.DC(1-5V.DC)信号制是国际电工委员会(IEC):过程控制系统(是连接仪表.变送设备.控制设备.计算机采样设备)用模拟信号标准.我国从DDZ-Ⅲ型电动仪表开始采用这一国际 ...

  2. ylbtech-LanguageSamples-XMLdoc

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-XMLdoc 1.A,示例(Sample) 返回顶部 “XML 文档”示例 本示例演示如 ...

  3. linux挂载U盘(转载)

    一.Linux挂载U盘:1.插入u盘到计算机,如果目前只插入了一个u盘而且你的硬盘不是scsi的硬盘接口的话,那它的硬件名称为:sda1.2.在mnt目录下先建立一个usb的目录(如:[root@lo ...

  4. java静态代理

    WorkIF.java package com.wzh.test; public interface WorkIf { void doWork(String name);} work.java pac ...

  5. Windows 32 程序设计

    C语言版 开发语言:C语言 开发工具:Visual Studio 2015 目      标:使用C语言,直接调用Windows API,创建Windows程序. 参考图书:<Windows程序 ...

  6. SyntaxError: Non-UTF-8 code starting with '\xba' in file 错误的解决方法!!

    第一次在Eclipse建立python工程,添加了自己新建的文件,写了一点代码,随后执行时候出现了错误,和昨天我在Visual Studio 2015里面一样,错误: SyntaxError: Non ...

  7. 解决mysql"Access denied for user'root'@'IP地址'"问题

    在另一台服务器使用 MySQL-Front链接时: 解决方法: 在MySQL服务器上使用root登录后,执行如下SQL语句: mysql 登录命令: >mysql -u root -p; 然后执 ...

  8. Web性能压力测试工具之Apache AB 详解

    下载安装地址: http://httpd.apache.org/download.cgi yum install httpd-tools http://www.apachelounge.com/dow ...

  9. JAVA中的策略模式

    现在我们有一个虚基类-鸭子(abstract Duck). 有真鸭子,野鸭子,橡皮鸭子继承了该类.虚基类有swing方法,毕竟游泳是所有的鸭子都应有的功能.还有一个虚方法display,这个方法在子类 ...

  10. 去掉 input type="number" 右边图标

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...