Valid Parentheses有效括号匹配。利用栈。
问题描述:给定一个字符串,其中只包含字符‘{’, '}', '[', ']', '(', ')'确定如果输入字符串是有效的。
括号必须以正确的顺序排列,“()”和“()[]{ }”都是有效的, "{", " {]"等都是无效的。
解题思路:利用栈,如果不是右括号就压入栈中,如果是右括号,就看前一个字符是不是匹配的左括号,如果匹配出栈,接着看后面字符,不匹配则返回false。
public boolean isValid(String s) {
Map<Character,Character> map = new HashMap<>();
map.put('(',')');
map.put('[',']');
map.put('{','}');
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i ++)
{
char curr = s.charAt(i);
if(map.keySet().contains(curr))
{
stack.push(curr);
}
//注意判断空的情况
else if(!stack.empty() && map.values().contains(curr))
{
char pre = stack.peek();
if(map.get(pre)==curr)
{
stack.pop();
}
else
{
return false;
}
}
else
{
return false;
}
}
return stack.empty();
}
Valid Parentheses有效括号匹配。利用栈。的更多相关文章
- 2.Valid Parentheses (括号匹配)
Level: Easy 题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- YTU 3003: 括号匹配(栈和队列)
3003: 括号匹配(栈和队列) 时间限制: 1 Sec 内存限制: 128 MB 提交: 2 解决: 2 [提交][状态][讨论版] 题目描述 假设一个表达式中只允许包含三种括号:圆括号&quo ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 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 ...
- OJP1147括号匹配加强版(栈)与P1153乱头发节(单调栈)
惨兮兮的被刷掉2%的通过率后在经过思考和dalao的指点后终于A掉了这道题 强烈建议修改这题的样例,实在太迷惑人,各种错误算法都能过 比如说这是一份错误代码,看懂了也不要学思路,和正解不知道差到哪里去 ...
随机推荐
- 【BZOJ1863】[Zjoi2006]trouble 皇帝的烦恼 二分+DP
[BZOJ1863][Zjoi2006]trouble 皇帝的烦恼 Description 经过多年的杀戮,秦皇终于统一了中国.为了抵御外来的侵略,他准备在国土边境安置n名将军.不幸的是这n名将军羽翼 ...
- Restoring Road Network
D - Restoring Road Network Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem State ...
- 巨蟒python全栈开发linux之centos1
1.linux服务器介绍 2.linux介绍 3.linux命令学习 linux默认有一个超级用户root,就是linux的皇帝 注意:我的用户名是s18,密码是centos 我们输入密码,点击解锁( ...
- hdu 4627 The Unsolvable Problem【hdu2013多校3签到】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4627 The Unsolvable Problem Time Limit: 2000/1000 MS ( ...
- after
.pay-type_select-after, .amount-item_select-after { padding: 0; border: @wx-width-one_unit solid @co ...
- Event Scheduler
MySQL :: MySQL 5.7 Reference Manual :: 23.4 Using the Event Scheduler https://dev.mysql.com/doc/refm ...
- DIV背景图片
.bigY{ position:absolute; width:95px; height:93px; visibility:visible; right: 277 ...
- 接口测试工具 — postman(post请求)
1.登录接口 2.添加学生信息,这个接口是用来讲入参是json类型的 3.学生金币充值接口,这个接口是为了讲添加cookie以及身份验证的 4.上传文件接口
- [转载]在table上使用::before/::after的问题
在table上使用::before/::after的问题 转载自: 次碳酸钴的技术博客 http://www.web-tinker.com/article/20638.html 在table上使用:: ...
- [NOIP2018PJ]摆渡车
[NOIP2018PJ]摆渡车 luogu mdPJ组这么难,还好考的TG组 先按t排序 设f[i][j]表示前i个人,第i个人等j分钟的最小总等待时间 这里j是小于2m的 可以考虑最坏情况下,一个人 ...