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. golang实现分布式缓存笔记(一)基于http的缓存服务

    目录 前言 cache 缓存服务接口 cache包实现 golang http包使用介绍 hello.go Redirect.go http-cache-server 实现 cacheHandler ...

  2. 条件随机场之CRF++源码详解-训练

    上篇的CRF++源码阅读中, 我们看到CRF++如何处理样本以及如何构造特征.本篇文章将继续探讨CRF++的源码,并且本篇文章将是整个系列的重点,会介绍条件随机场中如何构造无向图.前向后向算法.如何计 ...

  3. Spring 注解@Transactional readOnly=true

    引子 今天下班后,以前同事小胖问我Spring  Service类中的注解@Transactional readOnly=true的作用.做为他眼中的高人,我自然要装下A-C.居然想都没有想就说是注解 ...

  4. DB安装

    start from the execute file : DB2_ESE_10_Win_x86-64\setup.exe Navigator to "Install a Product&q ...

  5. jQueryPrint 的简单使用

    jQueryPrint 的简单使用 一.为什么要使用 jQueryPrint?  1.当然是方便的要死尼,相比于其他的方法. 2.打印整个页面或者局部页面都是非常的可以的,使用很方便. 3.如果要导出 ...

  6. BZOJ.3598.[SCOI2014]方伯伯的商场之旅(贪心 数位DP)

    题目链接 先考虑,对于确定的一个数,怎样移动代价最少(或者移到哪个位置最优)? 假设我们都移到下标\(1\)位置(设集合点为\(1\)),那么移动到下标\(2\)与\(1\)相比代价差为:\(下标&l ...

  7. 安装Git 创建版本库

    安装git [root@node1 ~]# yum -y install git 创建用户 因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址 [root@node ...

  8. MIRUO面试题

    1.c#可以继承string类吗?2.接口可以实现接口吗?抽象类可以实现接口吗?抽象类可以实现实体类吗?3.用C#计算2.5的3次方的方法.4.什么是协同程序?5.GC是什么,如何减少内存,如何加快性 ...

  9. iptables防火墙企业级模板

    目前公司业务已大多迁移至内网使用或者使用云主机,防火墙也渐渐不用了,在博客上记录一下,以免以后突然有用却找不到模板了.此防火墙脚本执行时默认清空旧的防火墙规则.放行本地loop网卡,DNS服务,NTF ...

  10. Linux 下安装 Mongodb

    mongodb在linux下面的安装应该是很简单的,但是有一个小点需要注意,这也就是我为什么写这篇博客的原因. 首先到其官网上下载最新稳定版,解压到目录,如/usr/local/mongodb 在mo ...