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

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

要求判断括号是否匹配,我们用一个stack来存储,然后用map存储对应的括号序列
每次往栈里添加元素时,通过map判断是左括号[还是右括号],如果是右括号,可以通过map判断出来,就从
stack中pop出栈顶元素来和当前括号匹配,如果不匹配则说明整个序列都不匹配,如果是左括号就继续加入到stack中
class Solution {
public boolean isValid(String s) {
Stack<Character> stack=new Stack<Character>();
Map<Character,Character> map=new HashMap<Character,Character>();
map.put(')','(');
map.put('}','{');
map.put(']','['); for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(map.containsKey(c)){
char temp=stack.empty()? '#':stack.pop();
if(temp!=map.get(c)) return false;
}else stack.push(c);
}
return stack.isEmpty();
}
}

[LeetCode]20. Valid Parentheses有效的括号的更多相关文章

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

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

  2. leetcode 20 Valid Parentheses 有效的括号

    描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...

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

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

  4. [LeetCode] 20. Valid Parentheses 验证括号

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

  5. [LeetCode] 20. Valid Parentheses 合法括号

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

  6. [leetcode]20. Valid Parentheses有效括号序列

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

  7. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  8. leetcode 20 Valid Parentheses 括号匹配

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

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

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

随机推荐

  1. Linux man语法结构说明

    一.man手册的内容结构(说明书页的格式): 标题含义: Name命令的名称和用途(摘要) Synopsis命令语法(摘要) Description完整描述 Environment命令使用的环境变量 ...

  2. SDUT OJ 图结构练习——最短路径 ( Floyed 算法 AND Dijkstra算法)

    图结构练习——最短路径 Time Limit: 1000 ms            Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  3. 深入解析Close()和Dispose()的区别

    很多人都认为Close()方法内部会调用Dispose()方法,所以并没有本质的区别!实际上这个看法不是很准确,对有 些类来说,的确Close()和Dispose()没有本质区别,但是对有些类来说并非 ...

  4. js 冒泡事件与解决冒泡事件

    事件冒泡 :当一个元素接收到事件的时候 会把他接收到的事件传给自己的父级,一直到window . html代码: <div id="div1"> <div id= ...

  5. How to: Create a Business Model in the XPO Data Model Designer

    How to: Create a Business Model in the XPO Data Model Designer This topic provides step-by-step inst ...

  6. Qt 学习之路 2(69):进程

    Qt 学习之路 2(69):进程 豆子 2013年11月9日 Qt 学习之路 2 15条评论 进程是操作系统的基础之一.一个进程可以认为是一个正在执行的程序.我们可以把进程当做计算机运行时的一个基础单 ...

  7. php 利用 json 传递数组之中文乱码最新解决办法

    json好用,但是如果数据中有中文就会出乱子了,网上解决办法多半是设置utf-8编码或转换字符编码 以下是我的解决办法,利用php的urlencode.urldecode函数(其实也是一种转换编码吧) ...

  8. Javascript 连接两个数组

    JS合并两个数组的方法 我们在项目过程中,有时候会遇到需要将两个数组合并成为一个的情况.比如: var a = [1,2,3]; var b = [4,5,6]; 有两个数组a.b,需求是将两个数组合 ...

  9. POJ - 2891 中国剩余定理

    \(mod\)存在不互素情况下的CRT #include<iostream> #include<algorithm> #include<cstdio> #inclu ...

  10. C# 一些请求的基类(待补充)

    using System.Runtime.Serialization; /// <summary> /// 通用分页请求类 /// </summary> [DataContra ...