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. Bmob后端云使用步骤

    1.登录创建应用后得到id 2.在清单文件中添加权限 <uses-sdk android:minSdkVersion="8" android:targetSdkVersion ...

  2. CentOS 6.4在运行XFS时系统crash的bug分析

    最近有一台CentOS 6.4的服务器发生多次crash,kernel version 是Linux 2.6.32-431.29.2.el6.x86_64.从vmcore-dmesg日志内容及cras ...

  3. shell find

    find   -name april*                     在当前目录下查找以april开始的文件 find    /   -amin    -10     # 查找在系统中最后1 ...

  4. saxon 处理xslt

    下载saxon : https://sourceforge.net/projects/saxon/?source=typ_redirect 下载后拿到: saxon9he.jar 运行CMD: C:\ ...

  5. django-访问控制

    django自带的用户认证系统提供了访问控制的的功能.   1.只允许登录的用户登录   django的用户可分为两类,一是可认证的用户,也就是在django.contrib.auth.models. ...

  6. AGC 001E.BBQ Hard(组合 DP)

    题目链接 \(Description\) 给定长为\(n\)的两个数组\(a,b\),求\[\sum_{i=1}^n\sum_{j=i+1}^n\binom{a_i+a_j+b_i+b_j}{a_i+ ...

  7. Python3字符串-最容易理解的方式

    字符串的创建 字符串创建符号 ' ' " " ''' ''' """ """ 转义符\ >>> str ...

  8. 早期(编译器)优化--Java语法糖的味道

    1.泛型与类型擦除 泛型的本质是参数化类型的应用,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口和泛型方法.在泛型没有出现之前,只能通过 ...

  9. maven的pom.xml配置文件中常用的配置标签解析(2018-03-13)

    来自:https://www.cnblogs.com/Nick-Hu/p/7288198.html 拿过来记录下 <project xmlns="http://maven.apache ...

  10. __NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000000003

    出现这个报错的原因是:拿数字与字符串进行对比了. 检查两边的数据格式是否一致 如果不一致,可以使用[nsstring stringwithformate:@"%d",xx]包装一下 ...