题目在这里: https://leetcode.com/problems/valid-parentheses/

【标签】Stack; String

【个人分析】这个题应该算是Stack的经典应用。先进后出 ( FILO) 的结构: 先来的左边括号跟后面的右边括号相匹配。

【代码解释】创建一个栈,如果遇到的是 '{', '(', '[', 就压到栈里面去,留着跟后面遇到的后边括号匹配。如果遇到了'}', ']', ')',首先看看栈里面 是不是空,里面有没有匹配的部分。

【一些心得】我在别人那儿看到一个很好的方法,可以通过增加一个全局map,来增加代码的扩展性: 假如还有其他类型的括号,直接加到map就可以。这样也可以使得代码简洁很多。

  /** add a global map to make code to be
* more extensible and more concise */
@SuppressWarnings("serial")
private static final Map<Character, Character> parentheseMap =
new HashMap<Character, Character>() {{
put('(', ')');
put('{', '}');
put('[', ']');
}
}; public boolean isValid(String s) {
int len = s.length();
if (len % 2 != 0) {
// for string of odd-number length, return false immediately
return false;
}
Stack<Character> lefts = new Stack<Character>();
for (char ch : s.toCharArray()) {
if (parentheseMap.containsKey(ch)) {
lefts.push(parentheseMap.get(ch));
} else {
// for '}', ']', ')',
// return false if nothing left or not matching
if ( lefts.isEmpty() || lefts.pop() != ch ) {
return false;
}
}
}
return lefts.empty();
}

[Leetcode][020] Valid Parentheses (Java)的更多相关文章

  1. LeetCode 020 Valid Parentheses

    题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...

  2. 【JAVA、C++】LeetCode 020 Valid Parentheses

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

  3. [LeetCode] 20. Valid Parentheses 合法括号

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

  4. [LeetCode] Longest Valid Parentheses 最长有效括号

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

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

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

  6. LeetCode: Longest Valid Parentheses 解题报告

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

  7. [Leetcode] longest valid parentheses 最长的有效括号

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

  8. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  9. [LeetCode] 20. Valid Parentheses 验证括号

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

随机推荐

  1. Symfony2源码分析——启动过程1

    本文通过阅读分析Symfony2的源码,了解Symfony2启动过程中完成哪些工作,从阅读源码了解Symfony2框架. Symfony2的核心本质是把Request转换成Response的一个过程. ...

  2. Jasper_filter data_pass field data from main to sub to filter some data

    main report: 1 add variable <variable name="Variable_rule" class="java.lang.String ...

  3. codevs 1068 乌龟棋

    题目描述 Description 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一 的起点,第N格是终点,游戏要求玩家控制 ...

  4. ImageView加ImageSwitch制作图片浏览器

    Main /** 图片浏览器*/public class MainActivity extends Activity implements ViewFactory{private Gallery ga ...

  5. favicon支持的图片格式

    为网站设置favicon有两种方式: 1.网站根目录下放置名为favicon.ico的图片,浏览器就会自动获取: 2.在页面中通过<link rel="shortcut icon&qu ...

  6. c++封装性

    C++ code到运行程序 作为一个c++程序员这个应该是最应该知道的细节,简言之:编译----链接----可执行的程序.这里所说的细节主要是第一步的细节,编译器如何把c++代码编译成目标代码.概括的 ...

  7. GitHub 如何基於 Node.js 和 Chromium 開發 Atom?

    看到回答里, 多数都没有回答到点子上, 还有些给了非常主观的意见而没有给出实际结论和分析过程. 题主的问题有四个: 1. Github 如何基于 Node.js 和 Chromium 开发 Atom? ...

  8. 九度OJ1486 /POJ 1029/2012北京大学研究生复试上机

    wa到死!wa到死!这是一个看着简单,坑及其多的题! 坑一:POJ上是单组输入,九度上是多组输入,妈蛋要是研究生复试遇到这种大坑肯定死掉啊!而且对于codeforces比较习惯的 同学肯定会觉得巨坑无 ...

  9. V$、GV$、X$、V_$、GV_$之间的关系

    V$.GV$.X$.V_$.GV_$之间的关系 GV$:全局视图,针对多个实例环境. V$:针对某个实例的视图. X$:是GV$视图的数据来源,oracle内部表. GV_$:是GV$的同义词. V_ ...

  10. Java向上转型注意事项

    继承.接口:Java子类中如果含有父类中不包含的变量与方法,子类对象向上转型时就是丢失这些变量和方法. interface SuperClass{ int i = 2; void f() ; } cl ...