Java [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.
解题思路:
Java现在不采用Stack栈了,那么我们用ArrayList替代之。
利用ArrayList来保存前括号,遇到后括号的情况就弹出ArrayList末尾数据,与之匹配。
若全部匹配,则最后是能够完全匹配的话,ArrayList应该为空。若不为空,则说明没有完全匹配。
代码如下:
public boolean isValid(String s) {
ArrayList<Character> list = new ArrayList<Character>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ')' || s.charAt(i) == ']' || s.charAt(i) == '}') {
if (list.isEmpty())
return false;
else {
char tmp = list.get(list.size() - 1);
list.remove(list.size() - 1);
if ((s.charAt(i) == ')' && tmp != '(')
|| (s.charAt(i) == ']' && tmp != '[')
|| (s.charAt(i) == '}' && tmp != '{'))
return false;
}
} else {
list.add(s.charAt(i));
}
}
return list.isEmpty();
}
Java [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 ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
随机推荐
- jQuery uploadify-v3.1 批量上传
引用: <link href="/UI.Web.CRM.Main/jQuery.Uploadify/uploadify.css" rel="stylesheet&q ...
- 【学习总结】OS X , IOS , IOS SDK , XCode之间的关系
几个基本的概念 : OS X : 属于桌面PC级别(IMac,MacPro等)对应安装的操作系统 IOS : 属于移动设备级别(Iphone,Ipad等)对应安装的操作系统 XCode: 是一个IDE ...
- 在百万数据中找出重复的数据sql
select * from (select count(name) as isone, name from tbl_org_departments group by name) t where t.i ...
- Timeline
Timeline面板 Chrome开发者工具详解(3)-Timeline面板 注: 这一篇主要讲解面板Timeline,参考了Google的相关文档,主要用于公司内部技术分享.. 更新时间:201 ...
- java程序练习:x进制转Y进制
/*X进制到Y进制转换*/ /*Step1.提示用户输入数据的进制X *Step2.接收用户输入的数据,保存到X * Scanner方法 *Step3.接收用户输入X进制的数据,保存到num *Ste ...
- 第二天就跳票 将wikipedia上的英文词条翻译为中文 手动
忙着改简历一整天,刚说完一天一博,就要跳票了. 还是写点东西吧. 今天又翻译了一个维基百科上的条目,刚过一天就忘了怎么弄,还得回头翻帖子.在这先记一下,省的以后找不到. 1.注册个wiki账号,轻松过 ...
- 深入js的面向对象学习篇(继承篇)——温故知新(三)
写这篇有关继承的文章时,突然想起,几天前的面试.因为习惯在学习知识的时候加上自己的理解,很喜欢用自己话来解释,于是乎当面试被问起继承原理时,噼里啪啦一大堆都是自己组织的话,(也可能是因为个人紧张.外加 ...
- [转载]C#导入XLS数据到数据库
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- PAT-乙级-1004. 成绩排名 (20)
1004. 成绩排名 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入n名学生的姓名.学号.成绩,分 ...
- Angular.js入门的样例
感觉这下子,前端的路也宽多了,从容不迫了. 因为可控制的节点又推前了, 有空了好好学一下. 然后结合EXPRESS或METEOR,是不是有点爽? 参考URL: https://toddmotto.co ...