当前位置:网站首页>『牛客|每日一题』有效括号序列
『牛客|每日一题』有效括号序列
2022-07-26 06:34:00 【starry陆离】
作者简介:一位喜欢写作,计科专业大二菜鸟
个人主页:starry陆离
首发日期:2022年7月17日星期日
上期文章:『牛客|每日一题』 栈的压入、弹出序列_
订阅专栏:『牛客刷题集锦』
如果文章有帮到你的话记得点赞+收藏支持一下哦

『牛客|每日一题』有效括号序列
1.每日一题

2.方法1:左括号入栈
2.1思路分析
遍历字符串,遇到'(','{','['这三种字符的时候,将字符入栈stk;而遇到')','}',']'这三种字符的时候则让对应的匹配字符出栈。具体规则如下:
- step 1:引入辅助栈stk,遍历字符串,每次遇到
'(','{','['字符的时候将字符入栈stk - step 2:当遇到
')','}',']'字符的时候,则检查栈是否空,且顶元素是否为匹配元素(如{和}匹配等),如果栈空或者栈顶元素不为匹配元素则括号序列不合法 - step 3:当栈非空,且栈顶元素为匹配元素,则栈顶元素出栈。
- step 4:循环匹配字符串,直到每次字符处理完
- step 5:检查栈stk是否为空,栈为空则序列合法,否则不合法(当括号以正确顺序关闭时则最后的栈为空)
2.2核心代码
import java.util.*;
public class Solution {
/** * @param s string字符串 * @return bool布尔型 */
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.方法2:右括号入栈
3.1思路分析
仍然是遍历字符串,借助辅助栈
- step 1:当遇到
'(','[','{'这三种字符的时候则让对应的匹配字符入栈(分别对应')',']','}') - step 2:当出现的字符不是
'(','[','{'这三种字符时,则先判断栈是否为空或者当前字符是否与栈顶元素一样, - step 3:当栈空或者当前字符与栈顶字符不一样时,则括号序列不合法,直接返回;
- step 4:否则栈顶元素出栈。遍历字符串直到所有元素遍历完成。最后判断栈是否为空,不为空则括号序列不合法;否则为合法序列。
3.2核心代码
import java.util.Stack;
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
//使用foreach循环
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();
}
}

订阅专栏:『牛客刷题集锦』
如果文章有帮到你的话记得点赞+收藏支持一下哦
边栏推荐
- Go的map字典及约束
- [image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
- 【无标题】
- [day_060423] convert string to integer
- Liberg avenue to Jane series
- The "darkest hour" has not come yet. Cherish every bullet 2020-03-22
- C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
- Conda 虚拟环境envs目录为空
- [day_040421] calculate candy
- English sentence pattern reference exclusive Edition - adverbial clause
猜你喜欢

Upgrade appium automation framework to the latest 2.0

Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言

【pytorch】图片增广

Cdga | how to build data asset catalogue?

Vision Transformer 必读系列之图像分类综述

09 eth smart contract

【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码

白盒测试的概念、目的是什么?及主要方法有哪些?

Distributed | practice: smoothly migrate business from MYCAT to dble
随机推荐
将金额数字转换为大写
@Constructorproperties annotation understanding and its corresponding usage
[fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code
How can machinery manufacturing enterprises do well in production management with the help of ERP system?
RNN recurrent neural network
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
深度学习——CV、CNN、RNN
PG Vacuum 杂谈之 auto vacuum
【Day05_0422】C语言选择题
Mobile web
Slice and array of go
Force buckle - 4. Find the median of two positive arrays
Basis of multimodal semantic segmentation
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
Do it yourself smart home: intelligent air conditioning control
TPS Motion(CVPR2022)视频生成论文解读
定义方法时为什么使用static关键字
Interpretation of TPS motion (cvpr2022) video generation paper
Go 的切片与数组
【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
