由Java实现Valid Parentheses
一、题目
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
二、解题思路
1、利用List集合实现一个栈;
2、将字符串s转换成字符数组,并循环遍历;
3、如果字符为:"{、(、["中的一个,则存入集合中;
4、如果字符为:"}、)、]"中的一个,则取出集合中最后一个元素进行比较;
5、如能匹配上,则删除集合中最后一个元素,否则返回false;
6、最后判断集合大小是否为0,如是则返回true。
三、代码实现
public boolean isValid(String s) {
if ("".equals(s)) {
return true;
} else {
Map parentheseMap = new HashMap();
parentheseMap.put(')', '(');
parentheseMap.put(']', '[');
parentheseMap.put('}', '{');
char[] sArr = s.toCharArray();
List stackList = new ArrayList();
for (int i = 0; i < sArr.length; i++) {
if (sArr[i] == '(' || sArr[i] == '[' || sArr[i] == '{') {
stackList.add(sArr[i]);
} else {无锡人流多少钱 http://wapyyk.39.net/wx/zonghe/fc96e.html
if (stackList.size() == 0) {
return false;
} else {
char temp = stackList.get(stackList.size() - 1);
if (temp == parentheseMap.get(sArr[i])) {
stackList.remove(stackList.size() - 1);
} else {
return false;
}
}
return stackList.size() == 0 ? true : false;
}
}
由Java实现Valid Parentheses的更多相关文章
- LeetCode第[20]题(Java):Valid Parentheses
题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 32. Longest Valid Parentheses (JAVA)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 32. Longest Valid Parentheses
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
随机推荐
- 统计Apache或nginx日志里访问次数最多的前十个IP
1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l 2.统计访问URL统计PV awk '{print $7}' access ...
- 单表:1.查询全部 2.条件查询 JSP Servlet
- WIN7 X64的运行命令窗口
要在WIN7系统下用界面的方式执行运行命令,则可以用如下两种方法方法打开运行对话框.1.点Win+R(run)就能出来的.2.在开始菜单上点右键,选“属性”,进入开始菜单属性设置界面,单击底部的“自定 ...
- fgetcsv()函数
fgetcsv()函数.fgetcsv()函数可以读取指定文件的当前行,使用CSV格式解析出字段,并返回一个包含这些字段的数组.语法格式如下:array fgetcsv(resource $handl ...
- Android的代码适配方案
public class DensityUtil { private DensityUtil(){ throw new AssertionError(); } /** * dp转px * @param ...
- 如何在Windows2008 Server服务器上开启Ping或者禁PING
方法1:命令行模式 进入服务器后 点击 开始--运行 输入命令: netsh firewall set icmpsetting 8 这样就可以在外部ping到服务器了 非常简单实用! 同样道理,如果想 ...
- ES6学习笔记(6)----函数的扩展
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ 函数的扩展 函数的默认值 : ES6可以为函数指定默认值 (1)指定默认值的两种方式 a.函数参 ...
- XDroidMvp 轻量级的Android MVP快速开发框架
XDroidMvp是XDroidAndroid快速开发框架的MVP版本,其使用方式类似于XDroid,大部分源码也来自XDroid. XDroidMvp主要会有这些特性: 无需写Contract! 无 ...
- Smart SVN的使用
最近项目使用了SVN,为管理代码起到了很好的作用!但是,对于很多初步使用着,还是非常不容易! 公司使用的是Smart SVN 客户端. Smart SVN 这个工具总体还是挺不错的! 在代码的提交和获 ...
- Handler引起的内存泄露
一般我都写handler的时候是这样的: public class MyActivity extends Activity{ private final Handler myHandler = n ...