[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 ...
随机推荐
- 求最小生成树(Prim算法)(1075)
Description 求出给定无向带权图的最小生成树.图的定点为字符型,权值为不超过100的整形.在提示中已经给出了部分代码,你只需要完善Prim算法即可. Input 第一行为图的顶点个数n ...
- Cstring类
GetLength: 获取CString类的对象包含的字符串的长度(字节数) IsEmpty: 测试CString类的对象包含的字符串是否为空 Empty: 使CString类的对象包含的字符串为空字 ...
- Linux查看一个文件夹大小
1.Linux查看一个文件夹大小: du -sh /home/yangkun [yangkun@sg1 bin]$ du -sh /home/yangkun/ 164M /home/yangkun/ ...
- android弹出式菜单、弹出式对话框、弹出式窗口
http://www.open-open.com/lib/view/open1389767042601.html http://www.open-open.com/lib/view/open13321 ...
- 专门讲讲这个MYSQL授权当中的with grant option的作用
对象的owner将权限赋予某个用户(如:testuser1) grant select ,update on bd_corp to testuser1 [with grant option ]1.如果 ...
- Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)
http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...
- Scala-数组
package com.mengyao.scala.function /** * Scala中数组的声明和使用(定长数组和变长数组) * * @author mengyao */object Tes ...
- StoryBoard页面联线跳转已经页面之间传参数
1.选中上图黄色.按住Control 把线拖到要要跳转的页面,寻找show. 2.选中联线.在右边Identifier:随便填入一个标示 3.在按钮点击事件加上如下代码 - (IBAction)but ...
- android后台截屏实现(1)--源码编译
前段时间接到任务要实现后台截图并上传的功能,在网上查了好久,发现遇到这类问题的人还不少.经过一番对比后发现还是修改并编译源码中的screencap类然后通过JNI来调用这种方法比较可靠,而其他的在ja ...
- HtmlParser 2.0 中文乱码问题
对于HTMLParser 2.0 工具包我们需要修改其中的Page.java文件使其适用中文的html文件分析. 主要是把protected static final String DEFAULT_C ...