【leetcode】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.
解题思路:
简单的栈的使用,前括号压栈,后括号弹栈。
class Solution:
# @return a boolean
def isValid(self, s):
sta = []
for i in s:
if i in ['(','[','{']:
sta.append(i)
elif i == ')':
if len(sta) < 1 or sta[len(sta)-1] != '(':
return False
else:
sta.pop()
elif i == ']':
if len(sta) < 1 or sta[len(sta)-1] != '[':
return False
else:
sta.pop()
else:
if len(sta) < 1 or sta[len(sta)-1] != '{':
return False
else:
sta.pop()
if len(sta) != 0:
return False
return True
【leetcode】Valid Parentheses的更多相关文章
- 【LeetCode】Valid Parentheses(有效的括号)
这道题是LeetCode里的第20道题. 题目要求: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭 ...
- 【LeetCode】Valid Parentheses合法括号
给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]( ...
- 【LeetCode】Generate Parentheses 解题报告
[题目] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【Leetcode】【Easy】Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
随机推荐
- Linux虚拟机中配置JDK环境变量
前提准备: 1,安装好Linux系统 2,下载好可以将文件传输到Linux系统工具例如:WinSCP 3,在windows中下载Linux版JDK: http://download.oracle.co ...
- Java学习笔记(未完待续)
变量的作用域(scope)是指变量可以在程序中引用的范围.在方法中定义的变量称为局部变量(local variable).局部变量的作用域从声明变量的地方开始,直到包含该变量的块结束为止.局部变量都必 ...
- OpenStack三种类型的NAT转换
SNAT SNAT即源网络地址转换,这个NAT路由修改IP包包头中的源IP地址.SNAT功能通常用于让只具有私有IP地址的主机能够访问外网,比如,多个PC使用路由器共享上网,每个PC都配置了内网IP, ...
- 大神php摘录
1. $rs = M('order')->where('id='.$res['id'])->save($order); )){ M('member')->where('id='.$m ...
- html5中画布和SVG的比较
SVG是基于XML的图形语言,在DOM解析中其每个元素都是可以用的,这样就可以为SCG元素附加JavaScript事件处理器,实现更加丰富的效果. 在SVG中,每个被绘制的图形均被视为对象,如果SVG ...
- PHP中Exception异常
异常的基本使用 当异常被抛出时,其后的代码不会继续执行,PHP 会尝试查找匹配的 "catch" 代码块. 如果异常没有被捕获,而且又没用使用 set_exception_hand ...
- 影响postgresql性能的几个重要参数
转载 一篇蛮老的文章了,但是还是很有用,可参考修补. PG的配置文件是数据库目录下的postgresql.conf文件,8.0以后的版本可支持K,M,G这样的参数,只要修改相应参数后重新启动PG服务就 ...
- Eclipse部署Maven web项目到tomcat服务器时,没有将lib下的jar复制过去的解决办法
我们在做web开发是,经常都要在eclipse中搭建web服务器,并将开发中的web项目部署到web服务器进行调试,在此,我选择的是tomcat服务器.之前部署web项目到tomcat进行启动调试都很 ...
- oracle DDL(数据定义语言)基本语句
--创建表格 create table production( ProductIdvarchar2(10), ProductNamevarchar2(20), ProductPricenumber( ...
- 【ZOJ 3929】Deque and Balls(普通dp)
题意:给出一个序列,按照顺序一个一个放入双端队列(可以放在头部也可以放在尾部),一个队列的美丽指数就是数列中a[i]>a[i+1]的个数,求美丽指数的期望*2^n的值. 解题思路:方便起见,我们 ...