当前位置:网站首页>Leetcode20. Valid parentheses
Leetcode20. Valid parentheses
2022-07-05 12:54:00 【accumulate steadily ض】
Valid parenthesis
This is also a classic problem of stack application
Input :s = "( )" Output :true Input :s = "( )[ ]{ }" Output :true
Input :s = "( ]" Output :false Input :s = "( [ ) ]" Output :false
Input :s = "{ [ ] }" Output :true
There are three kinds of brackets in this question 【】{} () Then these three can be mixed or not , See if it matches ?
Ideas :
The left parentheses are stacked , Then check whether the traversal character matches it , If it matches, pop up the left bracket at the top of the stack and continue to traverse , If there are elements in the last stack , Or there is a string that has not been traversed, but the stack is empty, which is not matched
class Solution {
public boolean isValid(String s) {
// If it's a left bracket, put it on the stack
Stack<Character> stack = new Stack<>();
for(int i =0;i<s.length();++i){
// If it's a left bracket, put it on the stack
if(s.charAt(i)=='('||s.charAt(i)=='['
||s.charAt(i)=='{'){
stack.push(s.charAt(i));
}else {
// If it's not the right parenthesis, compare it with the top element of the stack
if(stack.empty()){
// If the stack is empty , Can't compare
return false;
}else {
// The stack is not empty to see whether it is a match
if((stack.peek()=='('&&s.charAt(i)==')')
||(stack.peek()=='{'&&s.charAt(i)=='}')||
(stack.peek()=='['&&s.charAt(i)==']')){
stack.pop();
}else {
// If equal, pop up the top element
return false;
}
}
}
}
return stack.empty();
}
}
边栏推荐
- Using docker for MySQL 8.0 master-slave configuration
- Tips and tricks of image segmentation summarized from 39 Kabul competitions
- Kotlin变量
- 155. 最小栈
- Common commands and basic operations of Apache Phoenix
- 谈谈我写作生涯的画图技巧
- Rasa Chat Robot Tutorial (translation) (1)
- Kotlin variable
- 关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益
- 2021.12.16-2021.12.20 empty four hand transaction records
猜你喜欢
太方便了,钉钉上就可完成代码发布审批啦!
深度长文探讨Join运算的简化和提速
What if wechat is mistakenly sealed? Explain the underlying logic of wechat seal in detail
SAP UI5 视图里的 OverflowToolbar 控件
From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
开发者,云原生数据库是未来吗?
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
DNS的原理介绍
JDBC -- use JDBC connection to operate MySQL database
随机推荐
From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code
Taobao short video, why the worse the effect
Introduction to relational model theory
Alipay transfer system background or API interface to avoid pitfalls
Research: data security tools cannot resist blackmail software in 60% of cases
Simply take stock reading notes (2/8)
GPON other manufacturers' configuration process analysis
10 minute fitness method reading notes (3/5)
SAP UI5 FlexibleColumnLayout 控件介绍
Distributed cache architecture - cache avalanche & penetration & hit rate
NFT: how to make money with unique assets?
激动人心!2022开放原子全球开源峰会报名火热开启!
[cloud native] use of Nacos taskmanager task management
【Nacos云原生】阅读源码第一步,本地启动Nacos
Tips and tricks of image segmentation summarized from 39 Kabul competitions
Kotlin variable
Kotlin function
Time conversion error
超高效!Swagger-Yapi的秘密
Redis cluster configuration