原题链接

匹配括号

思路:

用栈,遍历过程中,匹配的成对出栈;结束后,栈空则对,栈非空则错。

Runtime: 4 ms, faster than 99.94% of Java

class Solution {
public boolean isValid(String s) {
Stack<Character> sta = new Stack<Character>(); for (int i = 0; i < s.length(); i++) {
char temp = s.charAt(i);
if (temp == '[' || temp == '(' || temp == '{') {
sta.push(temp);
} else {
if (sta.isEmpty())
return false;
char top = ' ';
if (temp == ']')
top = '[';
if (temp == ')')
top = '(';
if (temp == '}')
top = '{';
if (top == sta.peek())
sta.pop();
else
return false;
}
}
return sta.isEmpty() ? true : false;
} }

[leetcode] 20. Valid Parentheses (easy)的更多相关文章

  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 [Difficulty: Easy]

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

  7. leetcode 20 Valid Parentheses 括号匹配

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

  8. [LeetCode] 20. Valid Parentheses ☆

    转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...

  9. LeetCode 20 -- Valid Parentheses

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

随机推荐

  1. 【Qt】一劳永逸解决UAC问题(修改mkspecs\win32-msvc2012\qmake.conf)

    如果你的程序跑在一个开启了UAC保护的系统中,而你的程序又没有"盾牌"的话,程序总是会受到各种阻挠的,比如读写文件,写注册表等. 有了"盾牌"的话就不会出现一些 ...

  2. Qt4.8.6与VS2008的集成开发环境的安装配置

    一.安装编译Qt 1. 在Windows下用Qt做开发,编译器可以用mingw的gcc/g++,也可以用VS. 2. 安装VS2008集成开发环境(完全安装).  3. 安装qt4.8.6(qt-op ...

  3. Delphi 的RTTI机制浅探3(超长,很不错)

    转自:http://blog.sina.com.cn/s/blog_53d1e9210100uke4.html 目录========================================== ...

  4. 使用 acl_cpp 的 HttpServlet 类及服务器框架编写WEB服务器程序(系列文章)

    在 <用C++实现类似于JAVA HttpServlet 的编程接口 > 文章中讲了如何用 HttpServlet 等相关类编写 CGI 程序,于是有网友提出了 CGI 程序低效性,不错, ...

  5. 《C++ Primer》读书笔记 第一章

    读<C++ Primer>才知道,自己对C++知之甚少... 写个博客记录下自己C++的成长,只是读书笔记,不是对<C++ Primer>知识点的总结,而是对自己在书上看到的以 ...

  6. uni-app中vue组件父子值传递

    一.父组件向子组件传递数据(props) <template> <view class="container" style="background: # ...

  7. js中prototype与__proto__区别

    proto(隐式原型)与prototype(显式原型) 显式原型 explicit prototype property:每一个函数在创建之后都会拥有一个名为prototype的属性,这个属性指向函数 ...

  8. 【记录】mybatis中获取常量类中数据

    部分转载,已注明来源: 1.mybatis中获取常量类中数据 <update id="refuseDebt"> UPDATE dt_debt a SET         ...

  9. Codeforces Round #564 (Div. 2)B

    B. Nauuo and Chess 题目链接:http://codeforces.com/contest/1173/problem/B 题目 Nauuo is a girl who loves pl ...

  10. 我竟然不再抗拒 Java 的类加载机制了

    很长一段时间里,我对 Java 的类加载机制都非常的抗拒,因为我觉得太难理解了.但为了成为一名优秀的 Java 工程师,我决定硬着头皮研究一下. 01.字节码 在聊 Java 类加载机制之前,需要先了 ...