LeetCode 20 Valid Parentheses (括号匹配问题)
package leetcode_50; import java.util.Stack; /***
*
* @author pengfei_zheng
* 括号匹配问题
*/
public class Solution20 {
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
int len = s.length();
for(int i=0;i<len;i++){
char c = s.charAt(i);
if(c=='(' || c=='{' || c=='[') stack.push(c);
if(c==')'){
if(stack.isEmpty())
return false;
else if('('!=stack.pop())
return false;
}
if(c=='}'){
if(stack.isEmpty())
return false;
else if('{'!=stack.pop())
return false;
}
if(c==']'){
if(stack.isEmpty())
return false;
else if('['!=stack.pop())
return false;
}
}
if(!stack.isEmpty())
return false;
else
return true;
}
public static void main(String[]args){
String s="[[(])]";
System.out.println(isValid(s));
}
}
参考代码2:
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
return stack.isEmpty();
}
LeetCode 20 Valid Parentheses (括号匹配问题)的更多相关文章
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- 20. Valid Parentheses(括号匹配,用桟)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 20. Valid Parentheses - 括号匹配验证
Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- 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有效的括号
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 ...
随机推荐
- CentOS 7 64位更换内核安装锐速破解版
1.更新内核 rpm -ivh http://soft.91yun.org/ISO/Linux/CentOS/kernel/kernel-3.10.0-229.1.2.el7.x86_64.rpm - ...
- iOS 模拟器的“调试-位置”总是变成无的问题
选择项目的“Edit Scheme...” 并且选择“Options”选项卡,更改你喜欢的默认地理位置 你也可以编辑一个gpx文件永久保存坐标,或者在线生成(传送门). 转自:iOS Simulato ...
- Java编程思想学习笔记——字符串
前言 字符串操作是计算机程序设计中最常见的行为. 不可变String String对象是不可变的 重载"+"与StringBuilder String对象是不可变的,可以给Stri ...
- MongoDB 2.4企业版分析
作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs MongoDB v2.4版于3月19日发布,它引入了内置的文本搜索功能,以及基于哈希的分片和众所期盼的安全 ...
- 怎样把网站升级到http/2
https://juejin.im/post/59c63adf6fb9a00a4c271484
- 数据注解特性--NotMapped
NotMapped特性可以应用到领域类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列.. NotManpped特性打破了这个约定,你可以使用NotMa ...
- Android图片处理(Matrix,ColorMatrix) - 转载
Android图片处理(Matrix,ColorMatrix) 转载自:http://www.cnblogs.com/leon19870907/articles/1978065.html 在编程中有时 ...
- Java实现高效的枚举元素集合
Set是Java集合类的重要组成部分,它用来存储不能重复的对象.枚举类型也要求其枚举元素各不相同.看起来枚举类型和集合是很相似的.然而枚举类型中的元素不能随意的增加.删除,作为集合而言,枚举类型非常不 ...
- Java实现两个变量的互换(不借助第3个变量)
创建一个类,在该类中定义两个变量并为其指定初始值,然后交换两个变量的值,要求不允许借助第三个变量,只能使用异或运行实现两个变量值的交换. import java.util.Scanner; publi ...
- python中常用的知识
python中一切事务皆为对象. 所以我们看字符串.数字.集合等全部使用类的方法查看某一个对象. a = 'sb,2b' 查看对象是什么类型:print(type(a)) 查看此对象有哪些属性:dir ...