[Leetcode][020] Valid Parentheses (Java)
题目在这里: 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)的更多相关文章
- LeetCode 020 Valid Parentheses
题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...
- 【JAVA、C++】LeetCode 020 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode: Longest Valid Parentheses 解题报告
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- 手机端禁止iPhone字体放大
/*禁止iphone字体放大 */ html { -webkit-text-size-adjust: none; }
- linux 添加 $path
# vim /etc/profile在文档最后,添加:export PATH="/usr/local/src/bin:$PATH"保存,退出,然后运行:#source /etc/p ...
- IMAGE_SECTION_HEADER
typedef struct _IMAGE_SECTION_HEADER { BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; union { DWORD PhysicalAdd ...
- 数据分页SQL语句的比较
建立表 CREATE TABLE [TestTable] ( , ) NOT NULL , ) COLLATE Chinese_PRC_CI_AS NULL , ) COLLATE Chinese_P ...
- swift代码排版-参考
代码排版包括: 空行.空格.断行和缩进等内容.代码排版内容比较多工作量很多,但是非常重要. 空行 空行将逻辑相关的代码段分隔开,以提高可读性.下列情况应该总是添加空行: 类型声明之前. import语 ...
- 转:Keil MDK从未有过的详细使用讲解
来自:http://blog.csdn.net/zhzht19861011/article/details/5846510 熟悉Keil C 51的朋友对于Keil MDK上手应该比较容易,毕竟界面是 ...
- 体验Lua
想用之和NGINX结合,终结公司混乱的NGINX配置 玩起来先,感觉很精简,很实用哟. print("hello world") a={,} b=a print(a==b,a~=b ...
- 用MySQL创建数据库和数据库表
1.使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; +----------+ | Database | +----------+ | mysql ...
- Getting Text Metrics in Firemonkey(delphiscience的博客)
Firemonkey’s abstract TCanvas class has been providing the dimensions of the bounding rectangle of s ...
- ubuntu配置bridge网桥
先安装uml-utilities,该工具包含建立虚拟网络设备(所谓的“TAP interfaces”)的工具: sudo apt-get install uml-utilities 安装 桥接工具 b ...