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 "([)]" are not.


主要思想还是盏。

 public static boolean isValid(String s) {
HashMap<Character, Character> map = new HashMap<Character, Character>();
map.put('(', ')');
map.put('[', ']');
map.put('{', '}'); Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i); if (map.keySet().contains(curr)) {
stack.push(curr);
} else if (map.values().contains(curr)) {
if (!stack.empty() && map.get(stack.peek()) == curr) {
stack.pop();
} else {
return false;
}
}
} return stack.empty();
}
 public class S20 {  

     public static void main(String[] args) {  

     }  

     // 用stack来检查
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
// 如果遇到前括号就压入栈
if(c=='(' || c=='[' || c=='{'){
stack.push(c);
}else if(c==')' || c==']' || c=='}'){ // 遇到后括号就出栈
if(stack.size() == 0){ // 说明后括号太多了
return false;
}
char cpop = stack.pop();
if(cpop=='(' && c==')'){
continue;
}else if(cpop=='[' && c==']'){
continue;
}else if(cpop=='{' && c=='}'){
continue;
}
return false;
}
}
return stack.size()==0;
} }

LeetCode 20 -- Valid Parentheses的更多相关文章

  1. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  2. [LeetCode] 20. 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]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  6. leetcode 20 Valid Parentheses 括号匹配

    Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...

  7. [LeetCode] 20. Valid Parentheses ☆

    转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...

  8. Java [leetcode 20]Valid Parentheses

    题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  9. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

随机推荐

  1. Leetcode--Merge Two Sorted Lists

    static ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *temp = ); ListNode *head = te ...

  2. python——django入门篇

    要做一只有自学能力的pythoner,尽管大多数自学都是野生并不规范的,会遇到诸多坑,最后用稀奇古怪的方法解决了,但是先了解一些为以后真正学习道路填坑方便了简直不只一点点...重点来了:感觉以班里同学 ...

  3. 关于ViewPager、ViewFilpper、ViewFlow三种实现水平向滑动方式的比较

    ViewPagerViewPager类提供了多界面切换的新效果.新效果有如下特征:[1] 当前显示一组界面中的其中一个界面.[2] 当用户通过左右滑动界面时,当前的屏幕显示当前界面和下一个界面的一部分 ...

  4. Notepad++进行php开发所必需的插件

    Notepad++进行php开发所必需的插件有那些呢? 1. Compare: 可以用来比较两个文件不同之处. 2. Explorer:文件浏览器插件,包含收藏夹.Session保存功能.可与NppE ...

  5. php知识案列

     n个不重复的随机数生成代码 <?php //range 是将1到100 列成一个数组 $numbers = range (1,100); //shuffle 将数组顺序随即打乱 shuffle ...

  6. MVC MVP 和 MVVM的图示

    一直对于这些什么MVC MVP 和 MVVM都是云里雾里的 完全分不清楚 感觉jq上也没怎么用过,理解也很片面,画几张图也许能够大体分清他们之间的区别. 1.MVC(Model-View-Contro ...

  7. meta标签和作用

    <meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content=" ...

  8. python安装supervisor

    1.下载supervisor-3.3.1.tar.gz    http://pan.baidu.com/s/1dEPhGEd 2.下载meld3-1.0.2.tar.gz   http://pan.b ...

  9. Java线程(七):Callable和Future

    转自:http://blog.csdn.net/ghsau/article/details/7451464 本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果. C ...

  10. 前端神器Sublime Text3 常用插件&常用快捷键

    Sublime Text3常用插件 使用Package Control组件安装 也可以安装package control组件,然后直接在线安装: 按Ctrl+`调出console(注:安装有QQ输入法 ...