问题

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

参考答案

public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
return stack.isEmpty();
}

额外注释

这个代码很厉害。

首先建立一个 character 的 stack,作用是按照次序存储右半边的信息。等所有左半边的检查完毕,所有对应信息被push进stack,那么就可以开始检查右半边。

核心在于最后一个else if, 条件是『stack是否为空 or stack.pop 的值不等于检测值』,一旦其中一个成立,都返回false。

  • stack是空 -> 没有东西进入stack -> c不满足任何括号条件。
  • for循环 检查到右半边:最后被push被stack的信息 不等于 右半边开始的信息 -> 不对称 -> 返回 false

巧妙的是,判断完以后,stack最末尾的信息将会被pop出去,而最后结束了循环,如果stack全部被pop出去,那么stack为空,意味着,所有被推进stack的信息都被检查完毕,没有机会进入最后的 else if 条件。

大功告成。

LC 20 Valid Parentheses的更多相关文章

  1. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  2. 20. Valid Parentheses【leetcode】

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

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

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

  4. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  6. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  7. 刷题20. Valid Parentheses

    一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...

  8. C# 写 LeetCode easy #20 Valid Parentheses

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

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

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

随机推荐

  1. sqlserver 存储过程的新建与执行

    if Exists(select * from sysobjects where NAME = 'insert_custominfo' and type='P') drop procedure ins ...

  2. html 刷新更新背景图

    需求:每次刷新页面,随机获取背景图 实现方式: 1 通过js动态生成标签 <body> <script type="text/javascript"> va ...

  3. Spring Boot 中 Druid 的监控页面配置

    Druid的性能相比HikariCp等其他数据库连接池有一定的差距,但是数据库的相关属性的监控,别的连接池可能还追不上,如图: 今天写一下 Spring Boot 中监控页面的配置,我是直接将seat ...

  4. ICEM—非结构化周期网格

    原视频下载地址:https://yunpan.cn/cPBnmsNheJ46q  访问密码 3441

  5. JAVA基础知识|小知识点

    1.强烈建议,不使用char类型 那么,到底为什么java里不推荐使用char类型呢?其实,1个java的char字符并不完全等于一个unicode的字符.char采用的UCS-2编码,是一种淘汰的U ...

  6. nginx使用certbot配置https

    一般现在的网站都要支持https,即安全的http. 机器:阿里云Ubuntu 16.04.3 LTS 方案一:自己申请证书 配置时需要确保有ssl模块, 之后域名解析下, 之后时申请证书,可以去阿里 ...

  7. Linux下R环境安装

    R环境的两种安装方式,源码编译安装和yum在线安装 第一种:源码编译安装 1.首先,从官网上下载3.5.0版本 2.下载完后记得解压,我的习惯是解压在/usr/local下面 tar -zxvf R- ...

  8. windows把zookeeper注册成服务

    1.官网下载zookeeper:http://mirrors.hust.edu.cn/apache/zookeeper/. 2.修改zookeeper下面的文件/zookeeper/conf/zoo_ ...

  9. VUE数组操作方法的局限

    1.不能通过索引值直接设置一个项: vm.items[indexOfItem] = newValue 但是可以用set方法设置: Vue.set(example1.items,indexOfItem, ...

  10. SQL-W3School-基础:SQL AND & OR 运算符

    ylbtech-SQL-W3School-基础:SQL AND & OR 运算符 1.返回顶部 1. AND 和 OR 运算符用于基于一个以上的条件对记录进行过滤. AND 和 OR 运算符 ...