当前位置:网站首页>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
- 激动人心!2022开放原子全球开源峰会报名火热开启!
- Distance measuring sensor chip 4530a used in home intelligent lighting
- Database connection pool & jdbctemplate
- 非技术部门,如何参与 DevOps?
- Vonedao solves the problem of organizational development effectiveness
- SAP self-development records user login logs and other information
- [Nacos cloud native] the first step of reading the source code is to start Nacos locally
- 石臻臻的2021总结和2022展望 | 文末彩蛋
- SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子
猜你喜欢
Why is your next computer a computer? Explore different remote operations
Kotlin变量
Distributed cache architecture - cache avalanche & penetration & hit rate
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Transactions from December 27 to 28, 2021
A deep long article on the simplification and acceleration of join operation
[cloud native] use of Nacos taskmanager task management
从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
SAP UI5 视图里的 OverflowToolbar 控件
How to connect the API interface of Taobao open platform (super detailed)
随机推荐
Taobao order amount check error, avoid capital loss API
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
Distributed solution - distributed session consistency problem
Reshape the power of multi cloud products with VMware innovation
Transactions from December 27 to 28, 2021
Super efficient! The secret of swagger Yapi
JDBC -- extract JDBC tool classes
SAP SEGW 事物码里的 ABAP Editor
在家庭智能照明中应用的测距传感芯片4530A
Alipay transfer system background or API interface to avoid pitfalls
Introduction to the principle of DNS
CVPR 2022 | single step 3D target recognizer based on sparse transformer
关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
RHCSA4
Iterator details in list... Interview pits
Flume common commands and basic operations
Distributed cache architecture - cache avalanche & penetration & hit rate
Simply take stock reading notes (4/8)
Talk about my drawing skills in my writing career
insmod 提示 Invalid module format