【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 ...
随机推荐
- unity游戏开发新手-----2017年展望
0.希望三月份中旬之前找一份游戏开发的工作,必须转正; 1.希望存款3-4万; 2.今年年底结婚; 3.锻炼身体,体重保持在115斤左右,有胸肌和腹肌;(结婚之前实现) 4.技术方面: 熟练掌握C#语 ...
- dict与list的in 操作的速度
今天刷一道题,计算一串数字中其中两个数字相加等于目标值的题目,且取其中最早的两个数字(最后一个数字的位置靠前). 如[1,25,32,4,3,6,9,5] targer:9 输出 [3,6] ...
- SNMP协议以及著名的MIB详解
SNMP协议介绍 简单网络管理协议(SNMP:Simple Network Management Protocol)是由互联网工程任务组(IETF:Internet Engineering Task ...
- Opera 浏览器各版本下载地址
新版本下载地址: 正式分支: http://get.opera.com/ftp/pub/opera/desktop/ beta分支:http://get.opera.com/ftp/pub/opera ...
- gdb调试PHP扩展错误
有时候,使用PHP的第三方扩展之后,可能会发生一些错误,这个时候,可能就需要更底层的方式追踪调试程序发生错误的地方和原因,熟悉linux下C编程的肯定不陌生gdb 首先,使用ulimit -c命令,查 ...
- Lattice Reduction (LLL) 算法C代码实现
废话不多说,大名鼎鼎的Lenstra-Lenstra-Lovasz(LLL) 算法.实现参考论文:Factoring Polynomials with Rational Coefficients, 作 ...
- C# WinForm 技巧:COMBOBOX搜索提示
comboBox和textBox支持内置的搜索提示功能, 在form的InitializeComponent()中添加如下语句: this.comboBox1.AutoCompleteCustom ...
- HDU1020字符串操作
#include <stdio.h> #include <string.h> int N; int size; void main() { scanf("%d&quo ...
- eclipse中ctrl+h默认打开是JavaSearch,怎么设置成默认打开是FileSearch
window->preferences->General->keys. 找到File Search(有搜索框的,可以搜索),然后在下方 Binding按下ctrl +h .
- navigationController 的返回按钮自定义
1: navigationController 的返回按钮自定义 SecondViewController *secondVC = [SecondViewController new]; ...