LeetCode: Valid Parentheses 解题报告
Valid Parentheses
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.

SOLUTION1:
使用stack来解决的简单题目。所有的字符依次入栈
1. 遇到成对的括号弹栈,弹栈不成对返回false.
2. 栈为空只能压入左括号
3. 扫描完成时,栈应该为空,否则返回FALSE.
SOLUTION 2:
使用堆栈解决,比较简单。push右括号时,检查左括号在不在,如果不在,返回false,否则弹出一个左括号。
最后看栈是否为空,不为空代表括号数不对称,也要返回false;
public class Solution {
public boolean isValid(String s) {
if (s == null) {
return false;
}
int len = s.length();
// bug 1: don't use s as the name of the stack.
Stack<Character> stk = new Stack<Character>();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
switch(c) {
case '(':
case '[':
case '{':
stk.push(c);
break;
case ')':
if (!stk.isEmpty() && stk.peek() == '(') {
stk.pop();
} else {
return false;
}
break;
case '}':
if (!stk.isEmpty() && stk.peek() == '{') {
stk.pop();
} else {
return false;
}
break;
case ']':
if (!stk.isEmpty() && stk.peek() == '[') {
stk.pop();
} else {
return false;
}
break;
}
}
return stk.isEmpty();
}
}
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/stack/IsValid.java
LeetCode: Valid Parentheses 解题报告的更多相关文章
- LeetCode: Longest Valid Parentheses 解题报告
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- LeetCode: Valid Sudoku 解题报告
Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 上线---苹果AppStore审核注意事项,Guideline 1.2 - Safety - User Generated Content,2.1等条例(苹果审核六次拒绝)
前段时间上线app,和战友一起撸了那么久的代码,上线是最激动的.然而安卓各大平台上线了半个月了,苹果却给了六次拒绝. 刚开始等苹果等的焦头烂额,现在内心毫无波澜,目前还在审核中...... 六次的拒绝 ...
- Windows10下 tensorflow-gpu 配置
引言 越来越多的的人入坑机器学习,深度学习,tensorflow 作为目前十分流行又强大的一个框架,自然会有越来越多的新人(我也刚入门)准备使用,一般装的都是 CPU 版的 tensorflow,然而 ...
- 不要再用if(xxx != null)或者try catch NullPointerException了,Optional可以帮你解决
public static void testIfPresent() { Map<String, Map<String, String>> map = new HashMap& ...
- 使用perf工具导致系统hang死的原因
[perf工具导致系统hang住的原因是触发了低版本kernel的bug] 今天在测试服务器做压测,运行perf record做性能分析时,系统再次hang住了,这次在系统日志中记录了一些有用的信息, ...
- 【Ray Tracing The Next Week 超详解】 光线追踪2-8 Volume
Preface 今天有两个东东,一个是体积烟雾,一个是封面图 下一篇我们总结项目代码 Chapter 8:Volumes 我们需要为我们的光线追踪器添加新的物体——烟.雾,也称为participat ...
- 异构无线网络之QOS简介
QoS(Quality of Service,服务质量)指一个网络能够利用各种基础技术,为指定的网络通信提供更好的服务能力, 是网络的一种安全机制, 是用来解决网络延迟和阻塞等问题的一种技术. 在正常 ...
- PCB编译时出现的错误 ( Duplicate Net Names Wire N000-1 (Inferred)意思就是端口名字没有定义)
再运行这个错误Duplicate Net Names Wire N000-1 (Inferred)的就没有了, 不过还有其他的 错误,有错误不用怕,关我的博客解决.
- 深入一下Django的用户认证和cache
深入一下Django的用户认证和cache 用户认证 首先明白一个概念,http协议是无状态的,也就是每一次交互都是独立的,那如何让服务器和客户端进行有状态的交互呢,现在较为常见的方法就是让客户端在发 ...
- Android Studio 使用过程遇到的坑
最近在尝试Android Studio打Jar的包,然而事实并不是想象的那么简单,so,写多个坑的解决,以备不时之需. 1.Error:Execution failed for task ':app: ...
- linux目录的权限
Answer: When applying permissions to directories on Linux, the permission bits have different meanin ...