当前位置:网站首页>Valid bracket sequence of "Niuke | daily question"
Valid bracket sequence of "Niuke | daily question"
2022-07-26 06:35:00 【Starry Luli】
Author's brief introduction : One likes to write , Sophomore rookie of planning major
Personal home page :starry Lu Li
First date :2022 year 7 month 17 Sunday, Sunday
Last article :『 Cattle guest | A daily topic 』 Pressure into the stack 、 Pop-up sequence _
Subscription column :『 Collection of Niuke brush questions 』
If the article helps you, remember to praise + Collect and support

『 Cattle guest | A daily topic 』 Valid parenthesis sequence
1. A daily topic

2. Method 1: Left bracket in stack
2.1 Thought analysis
Traversal string , encounter '(','{','[' When these three characters , Put characters on the stack stk; And met ')','}',']' When these three characters are, the corresponding matching characters are sent out of the stack . The specific rules are as follows :
- step 1: Introducing auxiliary stack stk, Traversal string , Every encounter
'(','{','['Put characters on the stack stk - step 2: When you meet
')','}',']'When characters , Then check whether the stack is empty , And whether the top element is a matching element ( Such as { and } Match, etc ), If the stack is empty or the top element of the stack is not a matching element, the bracket sequence is illegal - step 3: When the stack is not empty , And the top element of the stack is a matching element , Then the element at the top of the stack will be released .
- step 4: Loop matching string , Until each character is processed
- step 5: Check stack stk Is it empty , If the stack is empty, the sequence is legal , Otherwise it's illegal ( When the parentheses are closed in the correct order, the last stack is empty )
2.2 Core code
import java.util.*;
public class Solution {
/** * @param s string character string * @return bool Boolean type */
public boolean isValid (String s) {
// write code here
Stack<Character>stk=new Stack<Character>();
int l=s.length();
for(int i=0;i<l;++i){
char a=s.charAt(i);
if(a=='('||a=='['||a=='{')stk.push(a);
if(a==')'){
if(stk.isEmpty()||stk.peek()!='(') return false;
stk.pop();
continue;
}
if(a==']'){
if(stk.isEmpty()||stk.peek()!='[')return false;
stk.pop();
continue;
}
if(a=='}'){
if(stk.isEmpty()||stk.peek()!='{')return false;
stk.pop();
continue;
}
}
return stk.isEmpty()?true:false;
}
}

3. Method 2: Right parenthesis on the stack
3.1 Thought analysis
Still traversal string , With the help of auxiliary stack
- step 1: When you meet
'(','[','{'When these three characters are, the corresponding matching characters are put on the stack ( They correspond to each other')',']','}') - step 2: When the character that appears is not
'(','[','{'When these three characters , First judge whether the stack is empty or whether the current character is the same as the top element of the stack , - step 3: When the stack is empty or the current character is different from the character at the top of the stack , The sequence of parentheses is illegal , Go straight back to ;
- step 4: Otherwise the top element of the stack is pushed out . Traverse the string until all elements are traversed . Finally, judge whether the stack is empty , If it is not empty, the bracket sequence is illegal ; Otherwise, it is a legal sequence .
3.2 Core code
import java.util.Stack;
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
// Use foreach loop
for (char c : s.toCharArray()) {
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
return stack.isEmpty();
}
}

Subscription column :『 Collection of Niuke brush questions 』
If the article helps you, remember to praise + Collect and support
边栏推荐
- dev treelist 常用用法小结
- Code runner for vs code, with more than 40million downloads! Support more than 50 languages
- What are the main reasons for server crash
- The "darkest hour" has not come yet. Cherish every bullet 2020-03-22
- Conda 虚拟环境envs目录为空
- [day_040421] calculate candy
- Map collection inheritance structure
- [day02_0419] C language multiple choice questions
- 【C语言】通讯录动态版和文件版
- Database and the future of open source
猜你喜欢

Basis of multimodal semantic segmentation

Alibaba cloud OSS binding custom domain name

C language file operation

【保姆级】包体积优化教程
![[pytorch] CNN practice - flower species identification](/img/af/81e2735ba385ba3d851e61a5fe2bef.png)
[pytorch] CNN practice - flower species identification

【pytorch】微调技术

【Day_06 0423】不要二

【无标题】

Decomposing a positive integer into prime factors requires decomposing into as many factors as possible.
![[untitled]](/img/42/5e8b62edc0aa289098425b26df2453.jpg)
[untitled]
随机推荐
排序问题:冒泡排序,选择排序,插入排序
[day_020419] inverted string
RNN循环神经网络
Jz36 binary search tree and bidirectional linked list
【Day_03 0420】数组中出现次数超过一半的数字
MySQL multi table query introduction classic case
[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you
BigDecimal becomes negative
YOLOv6:又快又准的目标检测框架开源啦
Go 的切片与数组
Map集合继承结构
[untitled]
Force buckle - 3. Longest substring without repeated characters
快速排序(quick-sort)
Yolov6: the fast and accurate target detection framework is open source
Servlet cannot directly obtain JSON format data in request request
PG Vacuum 杂谈之 auto vacuum
Deep learning - CV, CNN, RNN
Map dictionary and constraints of go
Gdown Access denied:Cannot retrieve the public link of the file.
