[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 ...
随机推荐
- Solr Update备注
参考资料: http://wiki.apache.org/solr/ExtractingRequestHandler#Sending_documents_to_Solr /update 标准的upda ...
- python 3.5 用户登录验证和输入三次密码锁定用户
#!/usr/bin/env python #encoding: utf-8 #登录程序,输入用户和密码输出欢迎信息,输入错误三次锁定用户,不让登录 import sys print (''' 欢迎登 ...
- navicat:cannot create oci environment
1.请注意红色箭头处的配置是从oracle官网下载的安装包 ,当然百度也是很强大的.(建议是在navicat安装目录下解压) 2.红色方框内的是你本地的oracle数据库的sqlplus.exe启动地 ...
- github使用入门 之GIT GUI Windows版
申明下是原创. 这二天网上也看了不少关于github使用的文章,github对代码管理也开始用起来了.这篇给github新手看,大牛们请跳过. github说白了就是版本管理库,最常用的就是程序代码管 ...
- PHP 类中的常量
类中的常量与静态成员类似他们只属于类而不属于类的任何实例,访问形式与访问静态成员一样. 例如: <?php class MyConst{ const RED="Red"; c ...
- Ruby on Rails Tutorial读书笔记-1
只是怕忘了命令,全部撸一次,记个大概.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 安装Ruby之前,先要安装RVM: curl -L https://get.rvm.io | bas ...
- JBOSS尝鲜
环境搭建:1. jdk-6u16-windows-i586.exe2. jboss-5.1.0.GA-jdk6.zip JDK安装: 安装过程很简单,应该都知道怎么安装软件....安装完JDK后,需要 ...
- 颠覆你的时空观-----理解傅立叶transform
在知乎上看到的,非常不错,最起码知道为什么了: 傅立叶变换,拉普拉斯变换,z变换这三种方法的本质主要就是将信号从时域转换成频域,因为频域更好展开分析 = =. 频域只是另一种看到信号的角度(世界观). ...
- 使用objdump objcopy查看与修改符号表
使用objdump objcopy查看与修改符号表动态库Linuxgccfunction 我们在 Linux 下运行一个程序,有时会无法启动,报缺少某某库.这时需要查看可执行程序或者动态库中的符 ...
- BZOJ 3153 Sone1
题解:水水哒AAA树啦 #include<iostream> #include<cstdio> #include<cmath> #include<algori ...