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 解题报告的更多相关文章

  1. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  2. LeetCode: Valid Palindrome 解题报告

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  3. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

  4. LeetCode: Valid Sudoku 解题报告

    Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...

  5. LeetCode: Valid Number 解题报告

    Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  6. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  9. [LeetCode] Longest Valid Parentheses 解题思路

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

随机推荐

  1. 二分搜索-poj2785

    题目链接:http://poj.org/problem?id=2785 题目大意:要求输入A,B,C,D四个数组,从每个数组中分别取出一个数来相加,求出相加后 和为0 总共有多少种加法. #inclu ...

  2. Linux学习之Vim/Vi使用(十三)

    Linux学习之Vim/Vi使用 Vim/Vi简介 Vim/Vi工作模式 Vim/Vi基本使用 Vim/Vi应用技巧 Vim/Vi简介 Vim/Vi是一个功能强大的全屏幕文本编辑器,是Linux/UN ...

  3. vue-particles粒子动画效果

    1.安装vue-particles依赖包 npm install vue-particles --save-dev 2.在main.js文件中引入并使用 import Vue from 'vue' i ...

  4. AGC027 E - ABBreviate

    目录 题目链接 题解 代码 题目链接 AGC027 E - ABBreviate 题解 神仙啊 建议查看https://img.atcoder.jp/agc027/editorial.pdf 定义a ...

  5. JVM的Client模式与Server模式

    概述 JVM有两种运行模式Server与Client.两种模式的区别在于,Client模式启动速度较快,Server模式启动较慢:但是启动进入稳定期长期运行之后Server模式的程序运行速度比Clie ...

  6. unity DoTween使用

    先说插件获取,DoTween是一个开源的插件,它的代码托管在Github上[https://github.com/Demigiant/dotween].若只是单纯项目需要是可以去AssetStore获 ...

  7. Redis集群官方推荐方案 Redis-Cluster

    Redis-Cluster redis使用中遇到的瓶颈 我们日常在对于redis的使用中,经常会遇到一些问题 1.高可用问题,如何保证redis的持续高可用性. 2.容量问题,单实例redis内存无法 ...

  8. [c#基础]ICloneable接口

    摘要 该接口使你能够创建现有对象的副本的自定义的实现.该接口只提供了,一个Clone方法,实现对象的浅拷贝.有浅拷贝,那么就有相对应的深拷贝.但该接口并没有对我们提供,需要我们自己实现. 什么是浅拷贝 ...

  9. Go语言之高级篇beego框架之config、httplib、context

    一.httplib 1.配置文件解析 这是一个用来解析文件的库,它的设计思路来自于 database/sql,目前支持解析的文件格式有 ini.json.xml.yaml,可以通过如下方式进行安装: ...

  10. [leetcode]Decode Ways @ Python

    原题地址:https://oj.leetcode.com/problems/decode-ways/ 题意: A message containing letters from A-Z is bein ...