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 ...
随机推荐
- linux学习之使用fdisk命令进行磁盘分区(八)
linux下使用fdisk命令进行磁盘分区 目录 分区类型 分区方法表示 文件系统 fdisk命令分区过程 分区类型 主分区:总共最多只能分四个 扩展分区:只能有一个,也算作主分区的一种,也就是说主分 ...
- 002.Open-Falcon Server部署及Agent监控
一 前期准备 节点 IP 备注 falcon 私网:172.24.10.95 临时公网:120.132.23.107 Open-Falcon服务端 node01 172.24.10.216 被监控端 ...
- Linux开源监控平台 -- Zabbix 小白安装以及使用
安装准备: 1.安装前需要先关闭selinux和firewall. 关闭Linux: [root@zabbix ~]# vi /etc/selinux/config 将SELINUX=enforcin ...
- C# 组件模组引用第三方组件问题
对接上一文章由于是动态加载指定程序集,会把当前目录下所有dll都加载进来.如果像sqlite这种第三组件调用了由C.C++非.net语言所以生成的Dll.因为自动生成的原因.会把非C#生成的dll都加 ...
- VUE 2.x SEO 优化问题 vue-meta-info && prerender-spa-plugin 配合使用
VUE 2.x SEO 优化问题,以及预渲染问题 1.新建项目可以采用nuxt.js , 配置meta.以及预渲染 都很方便,官网文档都很详细: 2.对于已有项目: vue-meta-info & ...
- @Resource 注解的使用
1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况 ...
- hihoCoder.1509.异或排序(位运算 思路)
题目链接 \(Description\) 给定长为\(n\)的序列\(A\).求有多少\(S\),满足\(0\leq S<2^{60}\),且对于所有\(i\in[1,n-1]\),\(a[i] ...
- memcached 一致性哈希算法
本文转载自:http://blog.csdn.net/kongqz/article/details/6695417 一.概述 1.我们的memcache客户端使用了一致性hash算法ketama进行数 ...
- asp.net c#并行调用service层代码
public ActionResult Home(AdviserSearchModel model) { //顾问列表需要的当前城市的下级地区 var ip = "117.82.196.19 ...
- 解决微信小程序ios端滚动卡顿的问题
方案1:直接使用微信小程序提供的 “scroll-view " 组件. <scroll-view scroll-y style="height: 100%;"> ...