Valid Parentheses leetcode java
题目:
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.
题解:
这道题是一道很常见的老题,记得当时看严蔚敏的数据结构还有数据结构课上面都见过这道题,一道训练栈的基础题。
解题方法是:
一个个检查给的characters,如果是左括号都入栈;如果是右括号,检查栈如果为空,证明不能匹配,如果栈不空,弹出top,与当前扫描的括号检查是否匹配。
全部字符都检查完了以后,判断栈是否为空,空则正确都匹配,不空则证明有没匹配的。
注意:
检查字符是用==,检查String是用.isEqual(),因为String是引用类型,值相等但是地址可能不等。
代码如下:
1 public boolean isValid(String s) {
2 if(s.length()==0||s.length()==1)
3 return false;
4
5 Stack<Character> x = new Stack<Character>();
6 for(int i=0;i<s.length();i++){
7 if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='['){
8 x.push(s.charAt(i));
9 }else{
if(x.size()==0)
return false;
char top = x.pop();
if(s.charAt(i)==')')
if(top!='(')
return false;
else if(s.charAt(i)=='}')
if(top!='{')
return false;
else if(s.charAt(i)==']')
if(top!='[')
return false;
}
}
return x.size()==0;
}
Valid Parentheses leetcode java的更多相关文章
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Valid Parentheses - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...
- Valid Parentheses [LeetCode 20]
1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- Longest Valid Parentheses - LeetCode
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Leetcode 20题 有效的括号(Valid Parentheses) Java语言求解
题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空 ...
- Generate Parentheses leetcode java
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- Valid Sudoku leetcode java
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- Valid Palindrome leetcode java
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Valid Number leetcode java
题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
随机推荐
- Category 特性在 iOS 组件化中的应用与管控
背景 iOS Category功能简介 Category 是 Objective-C 2.0之后添加的语言特性. Category 就是对装饰模式的一种具体实现.它的主要作用是在不改变原有类的前提下, ...
- JAVAEE——宜立方商城02:服务中间件dubbo、工程改造为基于soa架构、商品列表实现
1. 学习计划 第二天:商品列表功能实现 1.服务中间件dubbo 2.工程改造为基于soa架构 3.商品列表查询功能实现. 2. 将工程改造为SOA架构 2.1. 分析 由于宜立方商城是基于soa的 ...
- 支撑大规模公有云的Kubernetes改进与优化 (1)
Kubernetes是设计用来实施私有容器云的,然而容器作为公有云,同样需要一个管理平台,在Swarm,Mesos,Kubernetes中,基于Kubernetes已经逐渐成为容器编排的最热最主流的平 ...
- Linux进程管理子系统
<进程要素> <进程与程序的区别> 程序: 存放在硬盘上一些列代码和数据的可执行映像,是一个静止的实体 进程: 是一个执行中的程序,是动态的实体 <进程4要素> 1 ...
- Luogu 4492 [HAOI2018]苹果树 组合数
https://www.luogu.org/problemnew/show/P4492 找每个编号的点的父边的贡献,组合数和阶乘就能算了. 我考场上怎么就是没想到呢. 调了好久好久好久好久调不出来,样 ...
- Codeforces.739E.Gosha is hunting(DP 带权二分)
题目链接 \(Description\) 有\(n\)只精灵,两种精灵球(高级和低级),每种球能捕捉到第\(i\)只精灵的概率已知.求用\(A\)个低级球和\(B\)个高级球能捕捉到精灵数的最大期望. ...
- 【BZOJ】2760: [JLOI2011]小A的烦恼【字符串模拟】
2760: [JLOI2011]小A的烦恼 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 406 Solved: 258[Submit][Statu ...
- bzoj 2300 动态维护上凸壳(不支持删除)
新技能GET. 用set保存点,然后只需要找前趋和后继就可以动态维护了. /************************************************************** ...
- 通过IP获取所在城市
<script type="text/javascript"> var map = new BMap.Map("allmap"); var poin ...
- Java_基础知识回顾
1.ByteArrayInputStream. ByteArrayOutputStream String str = "ZHANGSAN"; //System.out.printl ...