当前位置:网站首页>678. 有效的括号字符串
678. 有效的括号字符串
2022-07-01 10:08:00 【hequnwang10】
一、题目描述
给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串。有效字符串具有如下规则:
- 任何左括号 ( 必须有相应的右括号 )。
- 任何右括号 ) 必须有相应的左括号 ( 。
- 左括号 ( 必须在对应的右括号之前 )。
- 可以被视为单个右括号 ) ,或单个左括号 ( ,或一个空字符串。
- 一个空字符串也被视为有效字符串。
示例 1:
输入: "()"
输出: True
示例 2:
输入: "(*)"
输出: True
示例 3:
输入: "(*))"
输出: True
二、解题
两次遍历
正反遍历,从左往右遍历,确定右括号,从右往左遍历,确定左括号。
class Solution {
public boolean checkValidString(String s) {
//两次遍历,正反遍历,先判断左括号是否有对应的右括号
int left = 0,cnt = 0;
int length = s.length();
char[] sch = s.toCharArray();
for(char ch : sch){
if(ch == '('){
left++;
}else if(ch == ')'){
left--;
}else{
cnt++;
}
//判断当前的左括号是否有对应的右括号
if(left < 0){
cnt--;
left++;
}
if(cnt < 0){
return false;
}
}
//从右往左遍历
int right = 0;
cnt = 0;
for(int i = length-1;i>=0;i--){
char ch = sch[i];
if(ch == ')'){
right++;
}else if(ch == '('){
right--;
}else{
cnt++;
}
//判断当前的右括号是否有对应的左括号
if(right < 0){
cnt--;
right++;
}
if(cnt < 0){
return false;
}
}
return true;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
贪心
从左到右遍历字符串,遍历过程中,未匹配的左括号数量可能会出现如下变化:
如果遇到左括号,则未匹配的左括号数量加 1;
如果遇到右括号,则需要有一个左括号和右括号匹配,因此未匹配的左括号数量减 1;
如果遇到星号,由于星号可以看成左括号、右括号或空字符串,因此未匹配的左括号数量可能加 1、减 1 或不变。
基于上述结论,可以在遍历过程中维护未匹配的左括号数量可能的最小值和最大值,根据遍历到的字符更新最小值和最大值:
如果遇到左括号,则将最小值和最大值分别加 1;
如果遇到右括号,则将最小值和最大值分别减 1;
如果遇到星号,则将最小值减 1,将最大值加 1。
任何情况下,未匹配的左括号数量必须非负,因此当最大值变成负数时,说明没有左括号可以和右括号匹配,返回 false。
当最小值为 0 时,不应将最小值继续减少,以确保最小值非负。
遍历结束时,所有的左括号都应和右括号匹配,因此只有当最小值为 0 时,字符串 s 才是有效的括号字符串。
class Solution {
public boolean checkValidString(String s) {
//贪心算法
//定义两个值,记录为匹配的左括号的最大值和最小值
int mincnt = 0,maxcnt = 0;
int length = s.length();
char[] sch = s.toCharArray();
for(int i = 0;i<length;i++){
char ch = sch[i];
if(ch == '('){
mincnt++;
maxcnt++;
}else if(ch == ')'){
mincnt = Math.max(mincnt-1,0);
maxcnt--;
if(maxcnt < 0){
return false;
}
}else{
mincnt = Math.max(mincnt - 1,0);
maxcnt++;
}
}
return mincnt == 0;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
边栏推荐
- Apple amplification! It's done so well
- Initial experience of Flink, a mainstream real-time stream processing computing framework
- button按钮清除边框
- 问下群里的各位,有使用flink oracle cdc的logminer方案,在生产上稳定运行的实际
- Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
- Swag init error: cannot find type definition: response Response
- 主流实时流处理计算框架Flink初体验
- 线程基础知识
- If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
- A quietly rising domestic software, low-key and powerful!
猜你喜欢
SQL server2014 failed to delete the database, with an error offset of 0x0000
北汽蓝谷:业绩承压,极狐难期
睡了二哥。。。
C [byte array] and [hexadecimal string] mutual conversion - codeplus series
What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in
Computer USB, HDMI, DP various interfaces and speeds
Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
全球基金和资管的股票建仓率达到15年内新低
Swag init error: cannot find type definition: response Response
Can you afford to buy a house in Beijing, Shanghai, Guangzhou and Shenzhen with an annual salary of 1million?
随机推荐
Apple amplification! It's done so well
How to understand JS promise
Swag init error: cannot find type definition: response Response
Eat a rich woman's melon...
Raspberry pie 4B system construction (ultra detailed version)
【Laravel 】faker数据填充详解
架构实战营 模块九:设计电商秒杀系统
在通达信上买基金安全吗?
A quietly rising domestic software, low-key and powerful!
tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
直播管理项目
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
“中移链”国密引擎在BSN正式上线
About database: how to avoid deadlock in gbase 8s
SQL server2014 failed to delete the database, with an error offset of 0x0000
What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
机器学习之线性回归详解
Is it safe to buy funds on the access letter?
SQL SERVER2014删除数据库失败,报错偏移量0x0000...