Valid Parentheses [LeetCode 20]
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]的更多相关文章
- Valid Parentheses - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Longest Valid Parentheses - LeetCode
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Valid Parentheses leetcode java
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
随机推荐
- JAVA 数组排序
一.数组升序排序 实例: import java.util.Arrays; //导入数组处理 public class Test{ public static void main(String[] a ...
- KVC , KVO , KVB
来源:http://www.cnblogs.com/jay-dong/archive/2012/12/13/2815778.html 熟悉oc语法的同学也许都会懂得这么一点:在oc中,类的成员变量或是 ...
- Oracle安全漏洞2016.10报告
Oracle安全漏洞2016.10报告 http://www.cnvd.org.cn/webinfo/show/3950
- 查看django里所有的url
>>> from django.core.urlresolvers import get_resolver >>> get_resolver(None).rever ...
- Ext 中xtype一览
基本组件: xtype Class 描述 button Ext.Button 按钮 splitbutton Ext.SplitButton 带下拉菜单的按钮 cycle Ext.CycleButton ...
- [SQL]SQL语言入门级教材_SQL语言基本语句介绍(四)
SQL语言基本语句介绍 • 表的建立 关系数据库的主要特点之一就是用表的方式组织数据.表是SQL语言存放数据.查找数据以及更新数据的基本数据结构.在SQL语言中,表有严格的定义,它是一种二维表,对于这 ...
- POJ 3270 【组合数学】
题意: 给长度为N的学列,然后让你通过置换来使其递增.原序列没有相同的数字. 1 ≤ N ≤ 10,000 ai<=100000 思路: 先找到循环,然后根据贪心只有两种比较好的情况,让循环里边 ...
- LoadRunner功能的Jmeter实现
- Android开发-API指南-<service>
<service> 英文原文:http://developer.android.com/guide/topics/manifest/service-element.html 采集(更新)日 ...
- Flex4+BlazeDS+JAVA+MySql 构建J2EE工程 对用户信息进行管理实例
要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...