java 使用Stack来判断Valid Parentheses
假如定义形如"{}[]()"或者"{[()]}"的模式为valid,"[{]"或者"(("的模式为invalid,那么我们可以使用一个stack或者递归下降的方法实现.
这里我先用stack实现一次.
实现的思路是.
当遇到开始符号时('[','{'或者'('),我们就将其push进栈。当遇到结束符号的时候,就pop盏里面的元素看是否匹配,否则返回false.
public boolean isValid(String s) {
char[] cs = s.toCharArray();
if (cs.length % 2 != 0)
return false;
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<cs.length;i++){
if(cs[i]=='[' || cs[i] == '(' || cs[i] == '{'){
stack.push(cs[i]);
}else{
if(stack.isEmpty()) return false;
switch (stack.pop()){
case '(':
if(cs[i]!=')') return false;
break;
case '[':
if(cs[i]!=']') return false;
break;
case '{':
if(cs[i]!='}') return false;
break;
}
}
}
if(!stack.isEmpty()) return false;
return true;
}
java 使用Stack来判断Valid Parentheses的更多相关文章
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- 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 ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- 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 (Stack; DP)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- 32. Longest Valid Parentheses (JAVA)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- vmwvare 网卡设置讲解
- HTTP Session学习
session在web开发中是一个非常重要的概念,这个概念很抽象,很难定义,也是最让人迷惑的一个名词,也是最多被滥用的名字之一,在不同的场合,session一次的含义也很不相同.这里只探讨HTTP S ...
- 选择移动web开发框架研究——有mui、frozenui以及Sencha Touch等
纯粹的总结一下移动web开发框架,移动 web开发框架有jQuery Mobile .Sencha Touch等等,他们都来源于web开发,是成熟的框架,jQuery Mobile出自于jQuery家 ...
- js原生之函数
1.函数作为参数传给其他函数: data.sort(function(a,b){return a-b}) //关于数组的sort函数,其回调函数返回负值,a在b之前 //正值,b在a ...
- iOS NSTimer
示例: //创建 scrollTimer =[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selecto ...
- Ionic在线打包IOS平台应用
参见:http://docs.ionic.io/services/profiles/#ios-app-certificate--provisioning-profile Ionic云编译,需要注册.地 ...
- Bootstrap入门(七)组件1:字体图标
Bootstrap入门(七)组件1:字体图标 包括200个来自 Glyphicon Halflings 的字体图标,允许 Bootstrap 免费使用. 部分可用图标截图: 所有图标都需要一个基类 ...
- mac下为gdb创建证书赋权其调试其它应用
1 使用/Applications/Utilities/Keychain Access.app创建证书 钥匙串访问->证书助理->创建证书 给证书随笔取一个名字,身份类型"自签名 ...
- 使用 visualstudio code 编辑器调试执行在 homestead 环境中的 laravel 程序
由于之前做 .net 开发比较熟悉 visualstudio,所以自 visualstudio code 发布后就一直在不同场合使用 vscode ,比如前端.node等等.最近在做 laravel ...
- oracle_数据库访问问题
1.通过JDBC连接数据库时报错“Caused by: oracle.net.ns.NetException: Got minus one from a read call” 网上百度得到: 1:数据 ...