LeetCode 20 -- 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.
主要思想还是盏。
public static boolean isValid(String s) {
HashMap<Character, Character> map = new HashMap<Character, Character>();
map.put('(', ')');
map.put('[', ']');
map.put('{', '}');
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i);
if (map.keySet().contains(curr)) {
stack.push(curr);
} else if (map.values().contains(curr)) {
if (!stack.empty() && map.get(stack.peek()) == curr) {
stack.pop();
} else {
return false;
}
}
}
return stack.empty();
}
public class S20 {
public static void main(String[] args) {
}
// 用stack来检查
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
// 如果遇到前括号就压入栈
if(c=='(' || c=='[' || c=='{'){
stack.push(c);
}else if(c==')' || c==']' || c=='}'){ // 遇到后括号就出栈
if(stack.size() == 0){ // 说明后括号太多了
return false;
}
char cpop = stack.pop();
if(cpop=='(' && c==')'){
continue;
}else if(cpop=='[' && c==']'){
continue;
}else if(cpop=='{' && c=='}'){
continue;
}
return false;
}
}
return stack.size()==0;
}
}
LeetCode 20 -- Valid Parentheses的更多相关文章
- 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 ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
- Java [leetcode 20]Valid Parentheses
题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- 51nod 1181 质数中的质数(质数筛法)
题目链接:51nod 1181 质数中的质数(质数筛法) #include<cstdio> #include<cmath> #include<cstring> #i ...
- 周爱民:真正的架构师是没有title的(图灵访谈)
周爱民,现任豌豆荚架构师,国内软件开发界资深软件工程师.从1996年起开始涉足商业软件开发,历任部门经理.区域总经理.高级软件工程师.平台架构师等职,有18年的软件开发与架构.项目管理及团队建设经验, ...
- #pragma pack(push,1)与#pragma pack(1)的区别
这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n) 作用:C编译器将按照n个字节对 ...
- "Couldn't communicate with a helper application" in Xcode 7
解决方案 xcrun git config --global user.email you@yourdomain.com xcrun git config --global user.name &qu ...
- 使用wait()与notify()实现线程间协作
调用sleep()和yield()的时候锁并没有被释放,而调用wait()将释放锁.这样另一个任务(线程)可以获得当前对象的锁,从而进入它的synchronized方法中.可以通过notify()/n ...
- Sql语句复习
1.两张表A,B 其中A表中字段1,字段2,字段3要迁移到B表中字段4,字段5,字段6,迁移后B表的字段7 全赋值为123: insert into B(字段4,字段5,字段6,字段7) select ...
- ExtJs 进度条(轮询)
客户端代码: Ext.onReady(function () { Ext.get('mb').on('click', function () { Ext.Ajax.request({ url: roo ...
- PHP是什么
php 是一种服务器端的,嵌入html的脚本语言.php区别其他像客户端java的地方是它的代码在服务器端执行.php能做什么? 最低水平,php可以做任何其他cgi程序所能做的事,例如收集表格数据, ...
- 个人纪录(初)----Ueditor文本编辑器的引用
源码下载地址:http://ueditor.baidu.com/website/ 打开源码参考.html 把ueditor引入自己的web项目下. 在需要引入文本的地方写个div,设置样式等. 并且在 ...
- iOS沙盒处理
沙盒文件 每个ios应用都有自己的应用沙盒,应用沙盒就是文件系统目录,与其他应用的文件系统隔离,ios系统不允许访问其他应用的应用沙盒.在ios8中已经开放访问. 应用沙盒一般包括以下几个文件目录:应 ...