由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 ...
随机推荐
- HTTP请求头中的那些东西
一.HTTP请求头是什么? HTTP请求头,HTTP客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST).如有必要,客户程序还可以选择发送其他的请求头. 二.HT ...
- firewall-cmd 使用总结
firewalld的简要说明: firewalld .firewall-cmd .firewall-offline-cmd它们Python脚本,通过定义的在/usr/lib/firewalld下面的x ...
- js封装xhr【重复造轮子】
仿jquery ajax,不过功能没那么多.贴代码 --------------------------------------分割线--------------------------------- ...
- bryce1010专题训练——划分树
1.求区间第K大 HDU2665 Kth number /*划分树 查询区间第K大 */ #include<iostream> #include<stdio.h> #inclu ...
- ACM_求第k大元素(两次二分)
求第k大 Time Limit: 6000/3000ms (Java/Others) Problem Description: 给定两个数组A和B,大小为N,M,每次从两个数组各取一个数相乘放入数组C ...
- h5-23-百度地图api
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- JAVA常用知识总结(四)——集合
先附一张java集合框架图 下面根据面试中常问的关于集合的问题进行了梳理: Arraylist 与 LinkedList 有什么不同? 1. 是否保证线程安全: ArrayList 和 LinkedL ...
- 488 Zuma Game 祖玛游戏
回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球.每一次,你可以从手里的球选一个,然后把这个球插入到一串球中的某个位置上 ...
- [转].NET 4 并行(多核)编程系列之二 从Task开始
本文转自:http://www.cnblogs.com/yanyangtian/archive/2010/05/22/1741379.html .NET 4 并行(多核)编程系列之二 从Task开始 ...
- 前端编辑神器---sublime text2
个人印象 之前一直在用dreamweaver,但是由于软件太大,加载速度慢,所以sublime text2作为一款跨平台的编辑器,它的优势就展现出来了,它本身小巧,支持代码高亮,语法提示,自动完成,自 ...