[Leetcode][020] Valid Parentheses (Java)

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

【标签】Stack; String

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

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

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

 1  /** add a global map to make code to be
 2      *  more extensible and more concise */
 3     @SuppressWarnings("serial")
 4     private static final Map<Character, Character> parentheseMap =
 5             new HashMap<Character, Character>() {{
 6             put(‘(‘, ‘)‘);
 7             put(‘{‘, ‘}‘);
 8             put(‘[‘, ‘]‘);
 9         }
10     };
11
12     public boolean isValid(String s) {
13         int len = s.length();
14         if (len % 2 != 0) {
15             // for string of odd-number length, return false immediately
16             return false;
17         }
18         Stack<Character> lefts = new Stack<Character>();
19         for (char ch : s.toCharArray()) {
20             if (parentheseMap.containsKey(ch)) {
21                 lefts.push(parentheseMap.get(ch));
22             } else {
23                 // for ‘}‘, ‘]‘, ‘)‘,
24                 // return false if nothing left or not matching
25                 if ( lefts.isEmpty() || lefts.pop() != ch ) {
26                     return false;
27                 }
28             }
29         }
30         return lefts.empty();
31     }
时间: 2024-10-27 01:15:48

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

[LeetCode] 020. Valid Parentheses (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 020.Valid_Parentheses (Easy) 链接: 题目:https://oj.leetcode.com/problems/valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个括号字符串是否是有效的. 分析:

LeetCode 020 Valid Parentheses

题目描述: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 "(]&qu

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

LeetCode: Longest Valid Parentheses [031]

0.下载安装Opencv,当前版本为249. 1.下载Python,当前OPencv版本为249,不过其支持的最新版本的Python为2.7,所以可以下载276版本. 2.下载numpy,开始我使用了1.6,没有通过,错误如图.下载了最新的1.8.1版本. 3.将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下. 4.找到opencv源文件内的draw.py运行. LeetCode: Lo

LeetCode: 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 "([)]"

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac

Leetcode Longest Valid Parentheses 结题报告

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[LeetCode] Longest Valid Parentheses 解题思路

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &