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 ...
随机推荐
- MySQL Connector Net连接vs2012问题
最近做一.NET项目,数据库用到MySQL,可是在VS2012连接数据库是遇到问题,提示:Authentication with old password no longer supported, u ...
- response 后刷新页面,点击按钮后,禁用该按钮
一,正常的点击按钮后,将其灰显,全部执行完毕再正常显示. this.btnSave.Attributes.Add("onclick", "if (typeof(Page_ ...
- JS 生成GUID
js 代码: function GUID() { this.date = new Date(); /* 判断是否初始化过,如果初始化过以下代码,则以下代码将不再执行,实际中只执行一次 */ if (t ...
- 第一好用的时间 日期插件(Adding a Timepicker to jQuery UI Datepicker)
最近在一个项目中用到了My97DatePicker,国人写的一个挺不错的时间选择插件,简单易用,但是在调试静态时却发现此插件必须产生一个iframe标签指向其主页,试了很多种方法都不能去除 ...
- PAT-乙级-1011. A+B和C (15)
1011. A+B和C (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 给定区间[-231, 231 ...
- Unity3D 自动打包整个项目(以AssetBundle实现)
原地址:http://blog.csdn.net/huang7jiao/article/details/18370653 需求: 在移动开发中,手动控制资源的加载.释放和热更新,是很有必要的. 而Un ...
- A JSTL primer, Part 2: Getting down to the core
In the initial article of this series, you got your first look at JSTL. We described the use of its ...
- POJ 3393 Lucky and Good Months by Gregorian Calendar
http://poj.org/problem?id=3393 题意 : 对于这篇长长的英语阅读,表示无语无语再无语,花了好长时间,终于读完了.题目中规定每周的周六日为假日,其他为工作日,若是一个月的第 ...
- xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误
性能分析:出现如下错误: xcode profile Variable has incomplete type class “CC_DLL” 解决办法:在 xcode的Build Setting ...
- AppStore 沙箱环境的测试流程
1:请确保你打得版本是 沙箱环境的版本 2:请确保的手机序列号已经加入沙箱环境3:请确保你的手机Apple ID 账户已经退出 ------ 如果这些都准备好了,再进行测试吧,不然一堆的未知问题等着你 ...