20. Valid Parentheses(stack)
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.
public class Solution {
if (s == null)
return false;
Stack<Character> stack = new Stack<Character>();
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if ((ch[i] == '(') || (ch[i] == '[') || (ch[i] == '{'))
stack.push(ch[i]);
else {
if (stack.isEmpty())
return false;
if (ch[i] - stack.pop() > 2)
return false;
}
}
return stack.isEmpty();
public boolean isValid(String s) {
if (s == null) //改进1:可以不用String.charat(i)
return false; //改进2: 先判断长度
//改进三: 不需要这么多if else ,符号的判断可以用ascii码
Stack<Character> stack = new Stack<Character>();
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if ((ch[i] == '(') || (ch[i] == '[') || (ch[i] == '{'))
stack.push(ch[i]);
else {
if (ch[i] == ')') {
if (stack.isEmpty())
return false;
else if (stack.pop() != '(')
return false;
}
if (ch[i] == ']') {
if (stack.isEmpty())
return false;
if (stack.pop() != '[')
return false;
}
if (ch[i] == '}') {
if (stack.isEmpty())
return false;
if (stack.pop() != '{')
return false;
}
}
}
if (!stack.isEmpty())
return false;
else
return true;
}
}
20. Valid Parentheses(stack)的更多相关文章
- [Leetcode] 20. Valid Parentheses(Stack)
括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- 32. Longest Valid Parentheses (Stack; DP)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- 20. Valid Parentheses (python版)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- Leetcode 之Longest Valid Parentheses(39)
有一定的难度.用堆栈记录下所有左符的位置,用变量记录下孤立右符的位置. int longestValidParentheses(const string& s) { stack<int& ...
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
随机推荐
- 利用Swift之协议语法实现页面间的传值功能
随着Swift 新开发语言的发布,又随着Xcode6.0.1的正式发布,利用swift编写iOS代码迫在眉睫,笔者在使用Objective-C开发近三年以来,对这种优雅的语法深感赞叹,下面我将对比式的 ...
- Storm0.9.4安装 - OPEN 开发经验库
Storm0.9.4安装 - OPEN 开发经验库 bin/zkServer.sh start /home/guym/down/kafka_2.8.0-0.8.0/config/zookeeper.p ...
- EmEditor处理大文本文件
前段时间新闻网由于用户不当操作.导致三年的报纸栏目内容全部清空.紧急情况下只能求助于SQL数据恢复.但备份的数据文件有500M左右. 首先用的文本编辑器是Notepad++,打开之后软件几乎完全卡死. ...
- Spring security与shiro
shiro更轻量级,spring security过于复杂. Apache Shiro 使用手册(一)Shiro架构介绍 Spring Security笔记:Remember Me(下次自动登录)
- 【性能诊断】六、并发场景的性能分析(windbg案例,大量的内部异常造成CPU飙升)
在做产品的某个核心模块的性能优化时,发现压到100并发时应用服务器的CPU就飙升至90%以上,50并发以后TPS就基本定格在一个数值上.使用性能监视器收集应用服务器的数据,发现每秒的.NET CLR ...
- DBA常用SQL之数据库基础信息
第一部分: 1. 查看oracle最大连接数 sql>show parameter processes #最大连接数 2. 修改最大连接数 sql>alter system set pro ...
- .git 目录文件介绍
$>tree -L 1.|-- HEAD # 这个git项目当前处在哪个分支里|-- config # 项目的配置信息,git config命令会改动它|-- des ...
- 在使用Redis的客户端连接工具ServiceStack.Redis要注意的问题
在使用Redis的客户端连接工具ServiceStack.Redis要注意的问题 Redis是一个非常NB的内存级的数据库,我们可以把很多”热数据“(即读写非常多的数据)放入其中来操作,这样就减少 ...
- SSH_框架整合6--修改Edit员工信息
SSH_框架整合6--修改Edit员工信息 1 加上修改Edit键 (1)emp-list.jsp <td> <a href="emp-input?id=${id }&qu ...
- php 自带函数
memory_get_usage()://查看当前内存使用情况单位 bytes str_repeat("liuhui", 2);//字符串重复指定次数,liuhui重复2次