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)的更多相关文章

  1. [Leetcode] 20. Valid Parentheses(Stack)

    括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...

  2. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  3. 32. Longest Valid Parentheses (Stack; DP)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

  5. 20. Valid Parentheses (python版)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

  7. Leetcode 之Longest Valid Parentheses(39)

    有一定的难度.用堆栈记录下所有左符的位置,用变量记录下孤立右符的位置. int longestValidParentheses(const string& s) { stack<int& ...

  8. 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

    [032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...

  9. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

随机推荐

  1. Java并发之CopyOnWriteArrayList

    CopyOnWriteArrayList是线程安全的.并且读操作无锁的ArrayList.不像ArrayList默认初始化大小为10的Object[],CopyOnWriteArrayList默认初始 ...

  2. XAMPP 在windows下无法启动Apache解决方案

    XAMPP 在windows下无法启动Apache解决方案 一.现象 XAMPP 点击Start Apache时出现如下错误 20:41:12  [Apache] Error: Apache shut ...

  3. 删除Android自带软件方法及adb remount 失败解决方案

    删除Android自带软件方法 1.在电脑上打开cmd,然后输入命令 adb remount adb shell su 2.接着就是Linux命令行模式了,输入 cd system/app 3然后输入 ...

  4. git设置代理

    git config --global https.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0. ...

  5. 学习C++11的一些思考和心得(1):lambda,function,bind和委托

     1.lambda表达式 lanbda表达式简单地来讲就是一个匿名函数,就是没有名称的函数,如果以前有接触过python或者erlang的人都比较熟悉这个,这个可以很方便地和STL里面的算法配合 st ...

  6. Mysql讲解数据库并发控制知识

    1.下载Mysql并安装,我喜欢不用安装的zip版,cd到bin目录下,先修改下mysql的密码. mysqladmin -u root -p password mysql ,第一次运行并修改mysq ...

  7. Java面试必备知识2

    1 .三个Statment区别,用法 Statement,基本的:PreparedStatement是可编译的,提高效率,callablestatement,存储过程 2 .Cookie 答:临时co ...

  8. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  9. [实变函数]4.2 Egrov 定理

    1 一致收敛很重要, 但可惜的是很多时候不一致收敛. 比如 $$\bex f_n(x)=x^n\to f(x)=\sedd{\ba{ll} 0,&x\in [0,1)\\ 1,&x=1 ...

  10. 前端实现 SVG 转 PNG

    http://fex.baidu.com/blog/2015/11/convert-svg-to-png-at-frontend/ 前言 svg 是一种矢量图形,在 web 上应用很广泛,但是很多时候 ...