题目在这里: 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. Java内部类和外部类的通信探索

    1.内部类访问外部类的成员和方法 在内部类中,可以无障碍地访问外部类的所有成员和方法. 在下面的实验代码中,可以看到,内部类sl可以访问外部类的私有成员:sz 和 cur. 同时可以访问私有方法:pr ...

  2. Android Recovery的汉化 显示中文

    Android Recovery的汉化 显示中文,有需要的朋友可以参考下. 首先下载开源的recovery源码,地址为https://github.com/xiaolu/android_bootabl ...

  3. Altium Designer 等长线&&蛇形线

    Altium Designer 里面怎么画等长线 (1)一般是将走线布完后,新建一个class. Design -> Classes 如上图添加完后可以点击close. (2)快捷键 T + R ...

  4. QTableWidget 用法总结(只能使用标准的数据模型,并且其单元格数据是QTableWidgetItem的对象)

    QTableWidget是QT程序中常用的显示数据表格的空间,很类似于VC.C#中的DataGrid.说到QTableWidget,就必须讲一下它跟QTabelView的区别了.QTableWidge ...

  5. android AudioRecorder简单心得

    1.如何创建一个有效的AudioRecorder实例 Android各种设备的采样频率不同,输入的声道数也不同,如果采用固定的采样频率和声道数,那么得到的AudioRecorder不一定能够正常初始化 ...

  6. BZOJ3296: [USACO2011 Open] Learning Languages

    3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  7. bzoj4096 [Usaco2013 dec]Milk Scheduling

    Description Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which ta ...

  8. Windows下重启指定名称的服务

    // 重启指定服务 void CPSSDPrinterCtrlPlug::RestartService(const wchar_t* nswServiceName) { SC_HANDLE schSC ...

  9. HDOJ 2191

    多重背包. 模版. #include <iostream> #include <stdio.h> #include <stdlib.h> #include < ...

  10. 解决 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

    "Accept-Encoding": "gzip, deflate", 这条信息代表本地可以接收压缩格式的数据,而服务器在处理时就将大文件压缩再发回客户端,IE ...