【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 ...
随机推荐
- 安装PHP sphinx扩展 sphinx-1.1.0/sphinx.c:105:2: error: too few arguments 错误
表现: /home/sphinx-1.1.0/sphinx.c: In function 'php_sphinx_client_read_property':/home/sphinx-1.1.0/sp ...
- JavaScript的Dom操作
两个练习: 1.两个表单里面的数据相互移动: <style type="text/css"> select { width: 100px; position: rela ...
- Spring学习记录1--@Transactional Propagation
起因 学习Spring的时候就知道aop有一个应用是声明式注解..反正往Service上一丢@Transactional就完事了..不用自己去开启hibernate的session,很简单. 但是@T ...
- phpstorm 使用技巧
专题1 专题2 专题3 专题4 快捷键
- mfc+vtk
MFC中view类主要处理显示视图,doc类处理文档,mainframe主要为整个窗口的和工程的设置管理.由此,VTK与MFC联合编程时,需要主要的是数据操作,以及显示要很好的与MFC中的结构结合,做 ...
- Windows无法完成安装,若要在此计算机上安装Windows,请中心启动安装。
现在安装系统已经很简单了,我觉得U盘启动的话两步就差不多了, 壹:设置BIOS,将U盘启动作为系统默认启动选项 贰:直接进去大白菜之类的,一键安装... 今天终于看到第三部了, 报错:Windows无 ...
- 【译文】JNI编程
原文链接: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html 没有逐字翻译,解说了文章的大 ...
- runtime
7.runtime实现的机制是什么,怎么用,一般用于干嘛. 你还能记得你所使用的相关的头文件或者某些方法的名称吗? 运行时机制,runtime库里面包含了跟类.成员变量.方法相关的API,比如获取类里 ...
- .net 面试基础题
Reference Link:http://www.yjbys.com/bbs/326026.html const关键字用来声明编译时常量,readonly用来声明运行时常量 密封类不能同时为抽象类 ...
- ubuntu14.04下搭建python+mysql环境
简略记录ubuntu14.04下搭建python操作的mysql服务器的过程和其中遇到的问题及解决方法. 第一部分: 安装mysql 安装步骤:1. sudo apt-get install mysq ...