转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html

描述

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

An input string is valid if:

Open brackets must be closed by the same type of brackets.
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

解析

括号匹配问题。

如果只有一种括号,我们完全可以用一个计数器 count ,遍历整个字符串,遇到左括号加 1 ,遇到右括号减 1,遍历结束后,如果 count 等于 0 ,则表示全部匹配。但如果有多种括号,像 ( [ ) ] 这种情况它依旧会得到 0,所以我们需要用其他的方法。类似大学里面写的计算器。

栈!

遍历整个字符串,遇到左括号就入栈,然后遇到和栈顶对应的右括号就出栈,遍历结束后,如果栈为空,就表示全部匹配。

代码

public boolean isValid(String s) {
Stack<Character> brackets = new Stack<Character>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '(':
case '[':
case '{':
brackets.push(c);
break;
case ')':
if (!brackets.empty()) {
if (brackets.peek() == '(') {
brackets.pop();
} else {
return false;
}
} else {
return false;
}
break;
case ']':
if (!brackets.empty()) {
if (brackets.peek() == '[') {
brackets.pop();
} else {
return false;
}
} else {
return false;
}
break;
case '}':
if (!brackets.empty()) {
if (brackets.peek() == '{') {
brackets.pop();
} else {
return false;
}
} else {
return false;
} }
} return brackets.empty();
}

[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

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

  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. _spellmod_aura_trigger

    一.spell.dbc做一个空光环 二.配置_spellmod_aura_trigger表 comment 备注 aura 光环ID,有此光环时,才会触发下面技能 linkSpell1-3 链接的技能 ...

  2. 独家 | 蚂蚁金服TRaaS技术风险防控平台解密

    小蚂蚁说: 在金融行业,风险防控能力的重要性不言而喻.而蚂蚁金服可实现高达99.999%的异地多活容灾,千亿级资金秒级实时核对“账.证.实”等能力也让业界有目共睹. 今年位于杭州的蚂蚁金服ATEC科技 ...

  3. java扫描文件夹下面的所有文件(递归与非递归实现)

    java中扫描指定文件夹下面的所有文件扫描一个文件夹下面的所有文件,因为文件夹的层数没有限制可能多达几十层几百层,通常会采用两种方式来遍历指定文件夹下面的所有文件.递归方式非递归方式(采用队列或者栈实 ...

  4. JCF

    点我 补两个红黑树原理:https://www.cnblogs.com/yyxt/p/4983967.html   https://www.cnblogs.com/skywang12345/p/324 ...

  5. CentOS 7系统安装配置图解教程

    操作系统:CentOS 7.3 备注: CentOS 7.x系列只有64位系统,没有32位.生产服务器建议安装CentOS-7-x86_64-Minimal-1611.iso版本 一.安装CentOS ...

  6. angular2 脏检查机制

    https://www.waitig.com/angular2-%E8%84%8F%E6%A3%80%E6%9F%A5%E8%BF%87%E7%A8%8B.html https://zhuanlan. ...

  7. RabbitMQ消息发布时的权衡

    在进行本篇文章的学习之前,你需要先阅读 https://www.cnblogs.com/duanjt/p/10057330.html.以便对Java访问RabbitMQ的基础用法有所了解. 一.失败通 ...

  8. Python的特殊成员

    Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用’from module import *’导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格:避免用下划 ...

  9. Synergy软件的基本配置/使用(详细教程)

    1.Synergy软件的简介 Synergy是一款可让多台电脑共享一个鼠标与键盘的软件,用户可借助Synergy操作一个鼠标与键盘控制多个电脑…… 2.Synergy软件的配置过程 下载链接:计算机相 ...

  10. xsd与xml和类(class)对象之间的互相转换

    xsd与xml和类(class)对象之间的互相转换 . 第一:通过现有的已经写好的xsd来生成class(.cs)文件. 在您Visual Studio的安装目录下的SDKv2.0Bin中有个应用程序 ...