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 " ...
随机推荐
- SpringMVC源码解读 - HandlerMapping - RequestMappingHandlerMapping初始化
RequestMappingHandlerMapping ,用于注解@Controller,@RequestMapping来定义controller. @Controller @RequestMapp ...
- iOS 11开发教程(十二)iOS11应用视图始祖——UIView
iOS 11开发教程(十二)iOS11应用视图始祖——UIView 在Swift中,NSObject是所有类的根类.同样在UIKit框架(UIKit框架为iOS应用程序提供界面对象和控制器)中,也存在 ...
- navicat premium 的使用——navicat 连接MySQL数据库
最近,在学习navicat premium这款第三方软件,他是一个三方数据库管理软件,在前一段时间实习的时候,我使用这款软件调用过服务器上的数据库,不过,当时并没有仔细研究,再次遇到这个软件,我决定仔 ...
- bzoj 4573: [Zjoi2016]大森林 lct splay
http://www.lydsy.com/JudgeOnline/problem.php?id=4573 http://blog.csdn.net/lych_cys/article/details/5 ...
- HDU 1692 Destroy the Well of Life 水题
Destroy the Well of Life Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- git pull的时候出错: Git Couldn't reserve space for cygwin's heap
具体: 1. 运行CMD,以管理员身份打开 2. 运行:rebase.exe -b 0x50000000 msys-1.0.dll 再次git pull的时候,不再报错 转自:http://doc.o ...
- HDU 4718 The LCIS on the Tree (动态树LCT)
The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Oth ...
- oracle 存储过程循环体中的return和exit区别:
oracle 存储过程循环体中的return和exit区别: (1) return 跳出整个循环,终止该循环, 后面的不再执行. 相当于 Java 中的break; (2) exit ...
- .Net C#上传文件最大设置
<!--网页允许的最大设置节点--> <system.web> <httpRuntime targetFramework="4.5" maxReque ...
- c#中何时使用Empty()和DefalutIfEmpty()
在项目中,当我们想获取IEnumerable<T>集合的时候,这个集合有可能是null.但通常的做法是返回一个空的集合. 假设有这样一个场景:当商店不营业时,返回一个空的IEnumerab ...