算法:匹配有效的括号20. Valid Parentheses
LeetCode全集请参考:LeetCode Github 大全
题目
20. Valid Parentheses
Given a string s containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Example 4:
Input: s = "([)]"
Output: false
Example 5:
Input: s = "{[]}"
Output: true
Constraints:
1 <= s.length <= 104
s consists of parentheses only '()[]{}'.
用栈Stack的解法
匹配左右括号,用栈解决,
- 左括号的时候入栈;
- 右括号的时候出栈,判断出栈的左括号 是否是同类型的,如果栈是空或者不匹配则返回false;
- 结束以后,Stack栈若是空则匹配true。
class Solution {public boolean isValid(String s) {// check edgeif (s == null || s.length() == 0) {return true;}Stack<Character> stack = new Stack<>();Map<Character, Character> map = new HashMap<>();map.put(')', '(');map.put('}', '{');map.put(']', '[');char[] chars = s.toCharArray();for(char c: chars) {if (c == '(' || c == '{' || c == '[') {stack.push(c);continue;}if (stack.isEmpty() || stack.pop() != map.get(c)) return false;}return stack.isEmpty();}
}
算法:匹配有效的括号20. Valid Parentheses相关推荐
- LeetCode 之 JavaScript 解答第20题 —— 有效的括号(Valid Parentheses)
Time:2019/4/11 Title: Valid Parentheses Difficulty: Easy Author: 小鹿 题目:Valid Parentheses Given a str ...
- C#LeetCode刷题之#20-有效的括号(Valid Parentheses)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4018 访问. 给定一个只包括 '(',')','{','}',' ...
- 20. Valid Parentheses 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- 20. Valid Parentheses
判断括号是否匹配 知道要用栈来做,但是过程还是想了一会儿,哎 1 bool isValid(char* s) { 2 int len = 0; 3 while(s[len++] != '\0'); 4 ...
- LeetCode: 20. Valid Parentheses
0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
题目: Given a string containing just the characters , determine if the input string is valid. The brac ...
- 20 Valid Parentheses
1 题目理解 输入:一个字符串s,只包含( ) { } [ ]这六种字符. 输出:字符串是否有效 规则:一个有效的字符串需要括号对应匹配,并且要左括号在前. 举例: 1 输入s="()&qu ...
- leetcode python3 简单题20. Valid Parentheses
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十题 (1)题目 英文: Given a string containing j ...
- 20.Valid Parentheses (python)
这道题主要用栈来实现的.什么是栈呢,参照书上的后缀表达式的例子谈谈自己的理解,栈最明显的特征是先进后出.所以可以有效的结合题目中 ()对匹配问题,可以把从列表中获取的符号先存到栈中. 首先建个空列表用 ...
- [LeetCode]--20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
最新文章
- 【C++多线程系列】【七】实现经典的C/S架构
- [云炬创业基础笔记] 第四章测试3
- python字符串截取_Python-变量-No3
- mysql自动增长id 溢出_MySQL表自增id溢出的故障复盘怎么解决 MySQL表自增id溢出的故障复盘解决方法...
- 内存中发堆和栈,栈是运行时的单位,而堆是存储的单位
- WEBAPI 增加身份验证 (OAUTH 2.0方式)
- 括弧匹配检验(信息学奥赛一本通-T1354)
- [OpenGL] Stencil Shadow Algorithm
- [swift] LeetCode 234. Palindrome Linked List
- VC++6遇到的问题(持续更新)
- java -cp 配置文件目录_java – 使用可执行JAR时指定Log4j2配置文件
- 洛谷P5369 [PKUSC2018]最大前缀和 [DP]
- iis7服务器发布网站,新手如何在服务器上用IIS部署网站
- 拳皇97c语言编码,拳皇97(格斗王97)
- springboot配置mysql
- spring security自定义登录失败返回错误信息
- MATLAB基础编程(004-01)在不同初速度和重力加速度下的的动态抛物运动绘图
- 【leetcode】小行星碰撞
- python怎么生成日志_python 生成模拟日志
- 清除浮动的四种样式写法
热门文章
- python管道怎么使用_如何在多个流程中正确使用管道(2)
- 单片机百位计数c语言,单片机c语言版数管动态显示实验报告.doc
- 解决IDEA中maven搭建web项目,在maven中有依赖但是在部署包中生成不出依赖
- 导致W3WP进程会重起情况
- 2019物联网博览会专业展览会-参加展会我们最专业
- 华为html如何自动换行,华为交换机不分页显示screen-length命令和screen-width每屏显示宽度-老肖的博客-51CTO博客,length怎么用...
- java中Map,List与Set的区别
- Java中String类型,int类型,double类型相互转换
- wordpress禁止恶意HTTP_USER_AGENT
- oracle重置口令是什么意思,Oracle重置数据库命令