[leetcode] 20. Valid Parentheses (easy)
原题链接
匹配括号
思路:
用栈,遍历过程中,匹配的成对出栈;结束后,栈空则对,栈非空则错。
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)的更多相关文章
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
- LeetCode 20 -- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- c# 将字符串转换为指定类型的值
private object GetValueByProperty(string key, string value, ref Type typeValue) { Type t = typeof(T) ...
- C#WinForm线程基类
在CS模式开发中一般我们需要用到大量的线程来处理比较耗时的操作,以防止界面假死带来不好的体验效果,下面我将我定义的线程基类给大家参考下,如有问题欢迎指正. 基类代码 #region 方法有返回值 // ...
- Memcached在Linux系统下的安装和PHP开启 Memcached的 扩展 超级解决方案
[项目背景]:阿里云ECS服务器,Linux(centos7.2 64位),环境部署使用的是阿里云一键安装包(LAMP)等 [项目需求]:linux安装memcached 和php开启Memcache ...
- vs中debug的一个小技巧 -- debug时忽略某段代码
#line 这是C#中的预处理命令 Visual Studio 2008 Visual Studio 2005 Visual Studio 2012 #line hidden 指令对调试器隐藏若干连续 ...
- BFS提高效率的一点建议
BFS有两种常见的形式: 形式1: 把初始点加入队列; while (队列非空) { 取出队头; 操作取出的点; 寻找周围符合条件的点加入队列; } 形式2: 操作初始点 把初始点加入队列; whil ...
- VS Code真机测试步骤
VS Code真机测试步骤 前提:你的电脑跟你的手机是在同一个网络环境下.电脑连手机热点: 1. 在扩展里搜索live server,下载安装: 2. 打开cmd 命令窗口(快捷键是win+r): 输 ...
- SpringBoot2.1.6 整合CXF 实现Webservice
SpringBoot2.1.6 整合CXF 实现Webservice 前言 最近LZ产品需要对接公司内部通讯工具,采用的是Webservice接口.产品框架用的SpringBoot2.1.6,于是采用 ...
- 【翻译】Keras.NET简介 - 高级神经网络API in C#
Keras.NET是一个高级神经网络API,它使用C#编写,并带有Python绑定,可以在Tensorflow.CNTK或Theano上运行.其关注点是实现快速实验.因为做好研究的关键是:能在尽可能短 ...
- Appium+python自动化(十七)- 你难道猴哥失散多年的混血弟弟 - Monkey简介之开山篇(超详解)
简介 今天由宏哥给小伙伴们来介绍猴哥的混血弟弟=Monkey.Monkey 是Android SDK提供的一个命令行工具, 可以简单,方便地运行在任何版本的Android模拟器和实体设备上. Monk ...
- c++最大公约数
C++辗转相除法求出最大公因数 样例输入 6 9 样例输出 3 程序 #include <stdio.h> using namespace std; int gcd(int m,int n ...