当前位置:网站首页>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();
}
}边栏推荐
- Reshape the power of multi cloud products with VMware innovation
- Vonedao solves the problem of organizational development effectiveness
- 从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
- Full text search of MySQL
- stm32和电机开发(从架构图到文档编写)
- Docker configures redis and redis clusters
- DNS的原理介绍
- stirring! 2022 open atom global open source summit registration is hot!
- Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
- SAP UI5 DynamicPage 控件介紹
猜你喜欢

How to connect the API interface of Taobao open platform (super detailed)

JSON parsing error special character processing (really speechless... Troubleshooting for a long time)

Taobao short videos are automatically released in batches without manual RPA open source

I'm doing open source in Didi

Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface

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

Transactions from January 14 to 19, 2022

Vonedao solves the problem of organizational development effectiveness

stm32和电机开发(从架构图到文档编写)

使用 jMeter 对 SAP Spartacus 进行并发性能测试
随机推荐
Comprehensive upgrade of Taobao short video photosynthetic platform
Free testing of Taobao tmall API order and flag insertion remark interface
Lepton 无损压缩原理及性能分析
Add a new cloud disk to Huawei virtual machine
从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
自然语言处理系列(一)入门概述
Taobao flag insertion remarks | logistics delivery interface
[Nacos cloud native] the first step of reading the source code is to start Nacos locally
Flume common commands and basic operations
GPON other manufacturers' configuration process analysis
HiEngine:可媲美本地的云原生内存数据库引擎
Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
Making and using the cutting tool of TTF font library
Simply take stock reading notes (1/8)
10 minute fitness method reading notes (3/5)
开发者,云原生数据库是未来吗?
Pinduoduo flag insertion remarks API
Pytoch counts the number of the same elements in the tensor
C alarm design
10 minute fitness method reading notes (2/5)