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. IO相关3(string流)

    sstream 头文件定义了三个类型来支持内存 IO,这些类型可以向 string 写入数据,从 string 读取数据,就像 string 是一个 IO 流一样. istringstream 从 s ...

  2. Django 错误:TypeError at / 'bool' object is not callable

    使用 Django自带的 auth 用户验证功能,编写函数,使用 is_authenticated 检查用户是否登录,结果报错: TypeError at / 'bool' object is not ...

  3. django自定义rbac权限组件(二级菜单)

    一.目录结构 二.表结构设计 model.py from django.db import models # Create your models here. class Menu(models.Mo ...

  4. 【离散数学】 SDUT OJ 谁是作案嫌疑人?

    谁是作案嫌疑人? Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 刑侦大队对涉及六个嫌 ...

  5. hdu 6196 搜索+剪枝

    Today, Bob plays with a child. There is a row of n numbers. One can takes a number from the left sid ...

  6. js 自定义属性

     html标签中有没有什么自带的属性可以存储成绩的----没有  本身html标签没有这个属性,自己(程序员)添加的,----自定义属性---为了存储一些数据  在html标签中添加的自定义属性,如果 ...

  7. hdu_1051 Wooden Sticks 贪心

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. tornado 03 请求与响应

    tornado 03 请求与响应 一.请求与响应 浏览器与服务器之间沟通的到底是什么信息 #服务器在后台一直保持运行着 #浏览器通过URL(路由.地址)发送请求 #服务器接收请求了通过tornado处 ...

  9. php 其他格式数据与数组互转

    class otherArr { private $char="UTF-8"; private $cvs_fege=","; // cvs 分割符 /**数组 ...

  10. 利用Python将文件进行分类整理

    利用Python将文件进行分类整理 功能 根据一个文件夹中的文件类型建立相应的文件夹,将同一种类型的文件放在一个文件夹中. 实现思路 主要用到 os 和 shutil 两个库,os 用来获取文件夹中的 ...