蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目
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.
翻译
括号匹配
Hints
Related Topics: Stack, String
通过栈就可以简单实现了
代码
Java
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for(char c : s.toCharArray()){
if(c == '(') stack.push(')');
else if(c=='[') stack.push(']');
else if(c=='{') stack.push('}');
else if (stack.isEmpty()||stack.pop()!=c)
return false;
}
return stack.isEmpty();
}
}
Python
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for i in range(len(s)):
if s[i]=='(' or s[i]=='[' or s[i]=='{':
stack.append(s[i])
elif s[i]==')' or s[i]==']' or s[i]=='}':
if len(stack)==0: return False
tmp = stack.pop()
if (s[i]==')' and tmp!='(') or (s[i]==']' and tmp!='[') or (s[i]=='}' and tmp!='{'):
return False
if len(stack)!=0:
return False
return True
蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]的更多相关文章
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- Matlab 装自定义模块
Matlab for Mac 右上角有一个set path选项. 点进去再点击 add with subfolders. 把你下载好的且解压过的工具箱添加进去 然后点save. 重启,就可以直接用了.
- 2016年JD工作遇到的问题:6-15,日常小问题
6.eclipse通过类名,找到所在的jar包. 解决办法:选中class, 快捷键 Ctrl +Shift+ T!!然后-- 之前经常是,根据类名找到对应的源文件,大脑短路,忘了下面的jar包提示. ...
- 【转载】COM 组件设计与应用(九)——IDispatch 接口 for VC6.0
原文: http://vckbase.com/index.php/wv/1224.html 一.前言 终于写到了第九回,我也一直期盼着写这回的内容耶,为啥呢?因为自动化(automation)是非常常 ...
- 【HNOI2014】画框
题面 题解 这又是一种套路啊233 将\(\sum a_i\)和\(\sum b_i\)分别看做\(x\)和\(y\),投射到平面直角坐标系中,于是就是找\(xy\)最小的点 于是可以先找出\(x\) ...
- matlab GUI工作原理
例如,用GUIDE创建名为ceshi的GUI程序,其m文件的主函数有如下形式.那么,打开该GUI时,它到底是怎么运行的呢?以下略作小结,欢迎大家补充 function varargout = cesh ...
- JAVAWEB和数据库 Mysql连接不上的原因及解决方案
有可能是安装了phpstudy或者wampserver这类自带mysql的web集成环境, 在关闭集成环境时误关了相对应的mysql服务,所以我们需要手动启动服务. 启动mysql的命令: net s ...
- 淡雅清新教师求职简历免费word模板
12款精美淡雅清新教师求职简历免费word模板,也可用于其他专业和职业,个人免费简历模板,个人简历表免费,个人简历表格. 声明:该简历模板仅用于个人欣赏使用,请勿用于商业用途,谢谢. 下载地址:百度网 ...
- Linux 技巧
Linux Handbook For RedHat Enterprise Linux System System # clean old kernel packages package-cleanup ...
- LintCode——尾部的零
尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...
- python继承与多继承
1.类与对象里的父类与子类(继承): 类的继承主要是指自子类对于之前父类的方法的继承,如果子类里面写了父类里的方法,则它会将父类里的方法覆盖掉,从而不能再调用到父类的方法. 2.为了解决父类与子类里的 ...