[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 ...
随机推荐
- Windows XP 每次开机都自动检测硬盘 解决办法(可以用HDDRegenerate修复坏道)
Windows XP,每次开机都自动检测硬盘,之前正常关机,没有任何非法操作.Windows XP,每次开机都自动检测硬盘,之前正常关机,没有任何非法操作. 1.和硬盘的分区格式有关,FAT32格式在 ...
- IntelliJ IDEA Maven工程保证JDK版本不变
创建maven项目后修改pom文件idea会默认将jdk版本调回到1.5,这是因为没有在pom里面设置项目的jdk版本 解决方法: 在pom文件中设定jdk版本即可,以下这种写法会自动更新idea中的 ...
- kubernetes之使用http rest api访问集群
系列目录 在Kubernetes集群中,API Server是集群管理API的入口,由运行在Master节点上的一个名为kube-apiserver的进程提供的服务. 用户进入API可以通过kubec ...
- python+Django 下JWT的使用
figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...
- 阿里云部署Redis服务器远程连接问题
昨天在阿里云免费领了一个月的云服务器,就着最近学的SpringBoot,准备做一个SpringBoot与Redis的整合. 因为以前用的Redis都是安装在本地的,使用过程中没遇到什么大问题,可是一旦 ...
- Matplotlib快速入门
Matplotlib 可能还有小伙伴不知道Matplotlib是什么,下面是维基百科的介绍. Matplotlib 是Python编程语言的一个绘图库及其数值数学扩展 NumPy.它为利用通用的图形用 ...
- 【朝花夕拾】Android自定义View篇之(八)多点触控(上)MotionEvent简介
前言 在前面的文章中,介绍了不少触摸相关的知识,但都是基于单点触控的,即一次只用一根手指.但是在实际使用App中,常常是多根手指同时操作,这就需要用到多点触控相关的知识了.多点触控是在Android2 ...
- mysql 正确清理binlog日志的两种方法
前言: MySQL中的binlog日志记录了数据库中数据的变动,便于对数据的基于时间点和基于位置的恢复,但是binlog也会日渐增大,占用很大的磁盘空间,因此,要对binlog使用正确安全的方法清理掉 ...
- code forces 1173 B. Nauuo and Chess
本文链接:https://www.cnblogs.com/blowhail/p/10991237.html B. Nauuo and Chess 原题链接:http://codeforces.com ...
- 高效 MacBook 工作环境配置,超实用!
作者:正鹏 & 隃墨 http://www.xialeizhou.com/?p=71 前言 工欲善其事,必先利其器,工具永远都是用来解决问题的,没必要为了工具而工具,一切工具都是为了能快速准确 ...