问题

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. 7.26T3小游戏

    小游戏 题目描述 有一个简单的小游戏.游戏的主人公是一个勇士,他要穿过一片黑森林,去 解救公主.黑森林的地图可以用一张 V 个点.E 条边的无向图来描述,起点是 1 号点,终点是 V 号点.勇士从起点 ...

  2. W tensorflow/core/platform/cpu_feature_guard.cc:45]

    W tensorflow/core/platform/cpu_feature_guard.cc:] The TensorFlow library wasn't compiled to use SSE3 ...

  3. tomcat控制前台到后台的乱码问题

    1.找到tomcat中的conf文件下的server.xml文件. 2.点击打开后找到 <Connector  port="8080" protocol="HTTP ...

  4. 指定版本下载yum离线安装包

    #!/bin/bash releasever=7 for i in salt-minion salt-api salt-master docker-ce docker-ce-cli docker-co ...

  5. app微信支付的集成步骤

    1.引用地址 //微信支付 compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' 2.注册 private IWXAPI api ...

  6. Visual Studio2013的C语言编译器对C99标准的支持情况

    Visual Studio2013终于开始比较良好地支持C99特性了.在此之前,如果用C语言写代码的话,变量名都需要放到函数体的前面部分,代码写起来十分别扭. 而Visual Studio2013中的 ...

  7. Scrapy - 小说爬虫

    实例解析 - 小说爬虫 页面分析 共有三级页面 一级页面 大目录 二级页面 章节目录 三级界面 章节内容 爬取准备 一级界面 http://www.daomubiji.com/ 二级页面xpath 直 ...

  8. mac下的夜神模拟器链接vscode

    1.找到夜神模拟器,点击右键,查看包内容,找到文件夹下面的macos在点击右键打开终端.输入: adb connect 127.0.0.1:62001 dart和flutter交流群:45289287 ...

  9. windows下gitee WEBHOOK的坑...

    折腾到凌晨五点,依然没有实现 windows下 分支  push之后服务器自动部署 主要是因为GIT的helper的配置上的问题.最后果断放弃了,最后祭出大招,用 WINDONS命令行自动循环..60 ...

  10. Spring Aop(十四)——Aop自动创建代理对象的原理

    转发地址:https://www.iteye.com/blog/elim-2398725 Aop自动创建代理对象的原理 我们在使用Spring Aop时,通常Spring会自动为我们创建目标bean的 ...