当前位置:网站首页>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();
}
}边栏推荐
- I met Tencent in the morning and took out 38K, which showed me the basic smallpox
- Four common problems of e-commerce sellers' refund and cash return, with solutions
- Neural network of PRML reading notes (1)
- Preliminary exploration of basic knowledge of MySQL
- stirring! 2022 open atom global open source summit registration is hot!
- 2021.12.16-2021.12.20 empty four hand transaction records
- Lepton 无损压缩原理及性能分析
- 使用 jMeter 对 SAP Spartacus 进行并发性能测试
- 单独编译内核模块
- Pytoch counts the number of the same elements in the tensor
猜你喜欢

SAP UI5 FlexibleColumnLayout 控件介绍

RHCAS6

Tips and tricks of image segmentation summarized from 39 Kabul competitions

Distributed cache architecture - cache avalanche & penetration & hit rate

Talk about my drawing skills in my writing career

在家庭智能照明中应用的测距传感芯片4530A

Transactions from January 14 to 19, 2022

A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this

SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子

UNIX socket advanced learning diary -ipv4-ipv6 interoperability
随机推荐
Kotlin函数
Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
Comprehensive upgrade of Taobao short video photosynthetic platform
155. 最小栈
Keras implements verification code identification
SAP UI5 FlexibleColumnLayout 控件介绍
研究:数据安全工具在 60% 的情况下无法抵御勒索软件
Transactions from January 6 to October 2022
Simply take stock reading notes (4/8)
SAP SEGW 事物码里的 Association 建模方式
谈谈我写作生涯的画图技巧
Iterator details in list... Interview pits
自然语言处理从小白到精通(四):用机器学习做中文邮件内容分类
Insmod prompt invalid module format
Distance measuring sensor chip 4530a used in home intelligent lighting
单独编译内核模块
How can non-technical departments participate in Devops?
超高效!Swagger-Yapi的秘密
SAP self-development records user login logs and other information
使用 jMeter 对 SAP Spartacus 进行并发性能测试