[抄题]:

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

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

没有左括号、左括号太多、不匹配

[思维问题]:

[一句话思路]:

不存在右括号太多的情况,因为右括号都是自己入栈的

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

不存在右括号太多的情况,因为右括号都是自己入栈的

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

stack:最晚来的左括号反而最先判断,有点“不公平”

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

22. Generate Parentheses 回溯法,又忘了

[代码风格] :

class Solution {
public boolean isValid(String s) {
//corner case
if (s == null) {
return true;
}
Stack<Character> stack = new Stack<Character>();
//push into stack
for (char c : s.toCharArray()) {
//left cases
if (c == '(') {
stack.push(')');
}else if (c == '[') {
stack.push(']');
}else if (c == '{') {
stack.push('}');
}else if (stack.isEmpty() || stack.pop() != c) {
//right case
return false;
}
}
return stack.isEmpty();
}
}

20. Valid Parentheses检验括号字符串的有效性的更多相关文章

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

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

  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. 20 Valid Parentheses(匹配括号)

    题目意思:判断一个字符串(){}[]是否符合 思路:用栈ps:实习一个多月了,代码也刷不动了,状态真不是一般的差 class Solution { public: bool isValid(strin ...

  5. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

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

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

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

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

  8. leetCode练题——20. Valid Parentheses

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

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

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

随机推荐

  1. bzoj 4552 [Tjoi2016&Heoi2016]排序——二分答案

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4552 二分答案,把 >= mid 的设成1.< mid 的设成0,之后排序就变成 ...

  2. 题目1413:神秘元素 ——lis的元素是否唯一

    求序列的最长子序列中不可分割元素的数目.不可分割元素,肯定属于某一个最长子序列,首先做的就是把属于最长子序列的数提取出来,减小查找范围.怎么提取?可以用LIS(最长递增子序列)和LDS(最长递减子序列 ...

  3. Brocade FC Switch 光信号强度查看

    步骤: 1.先运行,porterrshow 查看口状态,然后'statsclear' and 'slotstatsclear' 2.查看:porterrshow,看下port 口的情况 3.收集sup ...

  4. Protobuff java 文件生成命令

    protoc.exe -I./proto文件目录 --java_out=java文件目录 proto文件基于文件目录的全路径 protoc.exe -I./protoFolder --java_out ...

  5. HBase之五:hbase的region分区

    一.Region 概念 Region是表获取和分布的基本元素,由每个列族的一个Store组成.对象层级图如下: Table (HBase table) Region (Regions for the ...

  6. MySQL添加数据库的唯一索引的几种方式~

    创建表时直接设置: DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (  `stu_id` int(11) NOT NULL AUTO_IN ...

  7. 设计模式之——Composite组合模式

    上周面试,面试官问桥接模式是什么,我就举了个例子:手机分为苹果,小米....,每个手机都有视频,游戏...等功能.直观上是一个树形结构.这种情况下,可以用桥接模式,把手机作为接口,苹果,小米等继承手机 ...

  8. read/write/fsync与fread/fwrite/fflush的关系和区别

    read/write/fsync: 1. linux底层操作: 2. 内核调用, 涉及到进程上下文的切换,即用户态到核心态的转换,这是个比较消耗性能的操作. fread/fwrite/fflush: ...

  9. Servlet3.0的简单使用

    Servlet3.0(WEB3.0)算是比较新的Servlet技术了,对应的JavaEE版本是6,虽然目前最新的版本是3.1,对应版本JavaEE7.我们目前使用的做多的还是Servlet2.5的东西 ...

  10. [Python] Regular Expressions

    1. regular expression Regular expression is a special sequence of characters that helps you match or ...