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 ...
随机推荐
- 【网络】 NAT
NAT(Network Address Translation,网络地址转换)是1994年提出的.当在专用网内部的一些主机本来已经分配到了本地IP地址(即仅在本专用网内使用的专用地址),但现在又想和因 ...
- hbase-site.xml 参数设置
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...
- bnuoj 27874 "Center" of [p]erimeter midpoints(计算几何)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=27874 [题意]: 给你一个三角形三个顶点的坐标ABC,三角形各边取一点DEF,将三角形周长平均分割 ...
- 一个好用且方便的FastCgi C++库 - FastCgi++
不知道你是不是曾经发愁过使用FastCgi库来使用C++开发Fastcgi程序繁琐而且会与C++ STL代码产生冲突的地方,或者你还是习惯了cout而不是pringf,那这篇文章就可以了解到一个使用的 ...
- 1015: [JSOI2008]星球大战starwar - BZOJ
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- 1201: [HNOI2005]数三角形 - BZOJ
Description Input 大三角形的所有短边可以看成由(n+1)*n/2个单位三角形的边界组成.如下图的灰色三角形所示.其中第1排有1个灰色三角形,第2排有2个灰色三角形,……,第n排有n个 ...
- xfire for web-Service
1.0 XFire XFire是codeHaus组织提供的一个开源框架,它构建了POJO和SOA之间的桥梁,主要特性就是支持将POJO通过非常简单的方式发布成Web服务,这种处理方式不仅充分发挥了PO ...
- jQuery,javascript获得网页的高度和宽度
网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...
- Android开发之onClick事件的三种写法(转)
package a.a; import android.app.Activity; import android.os.Bundle; import android.view.View; import ...
- Hadoop将过时了?
http://www.kuqin.com/database/20120715/322528.html Hadoop这个单词如今铺天盖地,几乎成了大数据的代名词.仅仅数年时间,Hadoop从边缘技术迅速 ...