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. C# Winform下一个热插拔的MIS/MRP/ERP框架11(启航)

    初学时,有了想法却完全不知道该从何下指,此序列将抛砖引玉,与大家共同学习进步. 一个程序的初始,必然是启动. 我的要求: 1.应用程序保持单例: 2.从配置文件加载一些基础数据进行初始化: 3.显示软 ...

  2. Maven配置与安装

    最近重装了一下系统,便重新安装与配置了maven,记录这个过程并分享出来. 注意:maven安装需要Java依赖,我这里使用的是jdk1.8. 1.安装并配置环境变量 首先在 maven 官网下载 m ...

  3. Python正则表达式匹配日期与时间

    #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'Randy' import re from datetime import da ...

  4. selenium定位元素提示‘元素不可见’问题解决方法

    最近在使用selenium的过程中发现有元素能够在页面中查找到,但是pycharm中运行时始终报错element not visible,于是使用如下方法成功解决问题. 1.driver.find_e ...

  5. 冒泡排序 思想 JAVA实现

    已知一个数组78.75.91.36.72.94.43.64.93.46,使用冒泡排序将此数组有序. 冒泡排序是一个运行时间为O(N²)的排序算法. 算法思想:(已从小到大为例) 78.75.91.36 ...

  6. Qt 学习之路 2(31):贪吃蛇游戏(1)

    Qt 学习之路 2(31):贪吃蛇游戏(1) 豆子 2012年12月18日 Qt 学习之路 2 41条评论 经过前面一段时间的学习,我们已经了解到有关 Qt 相当多的知识.现在,我们将把前面所讲过的知 ...

  7. 【算法笔记】B1041 考试座位号

    1041 考试座位号 (15 分) 每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生 ...

  8. POJ_3069 Saruman's Army 【贪心】

    一.题面 POJ3069 二.题意分析 我的理解是,可以在每个点设置一个监测点,能够监测到范围R内的所有其他点,那么问给出N个点的一维位置,需要在其中挑多少个监测点把所有点都监测到. 贪心解决: 1. ...

  9. v-show, v-if, 以及动态组件的区别

    vue提供了v-if, v-show来动态显示隐藏组件 同时也提供了<component>元素在一个挂载点上动态的切换组件, 通过 is 来决定哪个组件被渲染显示 配合<keep-a ...

  10. [转] 使用 JavaScript 创建并下载文件

    [From] https://gaohaoyang.github.io/2016/11/22/js-create-file-and-download/ 本文将介绍如何使用 JavaScript 创建文 ...