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 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
随机推荐
- UCOS-信号标志组(学习笔记)
信号标志组即根据各任务的信号进行逻辑运算,根据逻辑运算的结果决定是否进行.发送方指定向那个标志组的哪一位(响应位等于1表明向哪一位发)发1还是0.等待逻辑结果的任务指定等待那个标志组的哪几位.这几位按 ...
- U盘安装Linux CentOS 6.5 64位操作系统(来自互联网)
从centOS6.5开始直接把iso文件写入u盘就行了. 方法1:windows平台:1.用UltraISO打开iso(如:CentOS-6.5-x86_64-bin-DVD1.iso)2.然后点“启 ...
- linux内核设计与实现--进程调度 系统调用
进程可以分为I/O消耗型和处理器消耗型. I/O消耗型指,进程的大部分时间用来提交I/O请求或者等待I/O请求. 处理器耗费型进程把时间大多用在执行代码上. linux采用了两种不同的优先级范围: 第 ...
- [AIR] AS3.0设置屏保功能
package com.controls { import flash.desktop.NativeApplication; import flash.events.Event; import fla ...
- Understanding Asynchronous IO With Python 3.4's Asyncio And Node.js
[转自]http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html Introduction I spent this su ...
- [ASP.NET]asp.net Repeater控件的使用方法
asp.net Repeater控件的使用方法 -- : 4770人阅读 评论() 收藏 举报 asp.netserveraspdatasetdeletexhtml 今天学习了,Repeater控件 ...
- ZOJ 3407 Doraemon's Cake Machine [数学]
题意: 最多有2000组测试样例,每组样例代表n,m; n代表要把蛋糕平分的份数,m代表必须进行多少次操作. 一共有三种操作 1.竖切 经过蛋糕圆心,将蛋糕整个向下切. 2.横切 平行于蛋糕平 ...
- CodeForces 604C 【思维水题】`
题意: 给你01字符串的长度再给你一个串. 然后你可以在这个串中选择一个起点和一个终点使得这个连续区间内所有的位取反. 求: 经过处理后最多会得到多少次01变换. 例如:0101是4次,0001是2次 ...
- cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
游戏暂停界面: cocos2d-x中游戏暂停界面提供的思路是用pushScene()和popScne(),即推进和弹出场景,当游戏暂停时,推进(pushScene())暂停场景,之前运行的场景将会自动 ...
- SpringMVC 注解事务
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactio ...