当前位置:网站首页>Leetcode 1249. 移除无效的括号(牛逼,终于做出来了)
Leetcode 1249. 移除无效的括号(牛逼,终于做出来了)
2022-06-25 10:55:00 【我不是萧海哇~~~~】

给你一个由 ‘(’、‘)’ 和小写字母组成的字符串 s。
你需要从字符串中删除最少数目的 ‘(’ 或者 ‘)’ (可以删除任意位置的括号),使得剩下的「括号字符串」有效。
请返回任意一个合法字符串。
有效「括号字符串」应当符合以下 任意一条 要求:
空字符串或只包含小写字母的字符串
可以被写作 AB(A 连接 B)的字符串,其中 A 和 B 都是有效「括号字符串」
可以被写作 (A) 的字符串,其中 A 是一个有效的「括号字符串」
示例 1:
输入:s = “lee(to)de)”
输出:“lee(to)de”
解释:“lee(t(co)de)” , “lee(tode)” 也是一个可行答案。
示例 2:
输入:s = "a)b(c)d"
输出:"ab(c)d"
示例 3:
输入:s = "))(("
输出:""
解释:空字符串也是有效的
提示:
- 1 <= s.length <= 10^5
- s[i] 可能是 ‘(’、‘)’ 或英文小写字母
主要思路:自定义一个结构体,用来记录’(‘和‘)’的位置信息
然后删除有效的括号,最后剩下的就是多余的’(‘和’)',然后从原始字符串遍历,跳过这些多余的括号就是答案。
Code:
class Solution {
public:
string minRemoveToMakeValid(string s) {
typedef struct
{
char temp;
int pos;
}param;
vector<param>vec;
for(int i=0;i<s.length();i++)
{
if(s[i]=='(' || s[i]==')')
{
param p;
p.temp=s[i];
p.pos=i;
vec.push_back(p);//记录括号信息
}
}
if(vec.size()==0)
return s;
//以下步骤是删除有效的括号
vector<param>vec2;//这个数组最后剩下的不合法的括号信息
for(int i=0;i<vec.size();i++)
{
if(vec2.size()==0)
{
vec2.push_back(vec[i]);
continue;
}
param p=vec2.back();
if(p.temp=='(')
{
if(vec[i].temp==')')
{
vec2.pop_back();
cout<<"-++"<<endl;
}
else
vec2.push_back(vec[i]);
}
else
{
vec2.push_back(vec[i]);
}
}
for(int i=0;i<vec2.size();i++)
{
cout<<vec2[i].pos<<endl;
}
int loop=0;
string res;
for(int i=0;i<s.length();i++)
{
if( (loop!=vec2.size())&& i==vec2[loop].pos )
{
loop++;
}
else
{
res+=s[i];
}
}
cout<<"res="<<res<<endl;
return res;
}
};
边栏推荐
- 成长:如何深度思考与学习
- Detailed explanation of Android interview notes handler
- Introduction to socket UDP and TCP
- Tidb applicable scenarios
- Gaussdb cluster maintenance case set - slow SQL execution
- Daily Mathematics Series 52: February 20
- 仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)
- 看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
- 输出式阅读法:把学到的知识用起来
- 开源社邀请您参加OpenSSF开源安全线上研讨会
猜你喜欢

开源社邀请您参加OpenSSF开源安全线上研讨会

報名開啟|飛槳黑客馬拉松第三期如約而至,久等啦

Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance

1-7Vmware中的快照与克隆

网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目

【观察】ObjectScale:重新定义下一代对象存储,戴尔科技的重构与创新
![[image fusion] image fusion based on morphological analysis and sparse representation with matlab code](/img/ae/027fc1a3ce40b35090531370022c92.png)
[image fusion] image fusion based on morphological analysis and sparse representation with matlab code

网络远程访问的方式使用树莓派

Démarrer avec Apache shenyu

Advanced single chip microcomputer -- development of PCB (2)
随机推荐
Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书
撸一个随机数生成器
某APP中模拟器检测分析
[the path of system analyst] Chapter 6: Double inventory demand engineering (comprehensive knowledge concept)
Apache ShenYu 入门
好好思考
Garbage collection mechanism
Get to know Prometheus
开源社邀请您参加OpenSSF开源安全线上研讨会
Google Earth Engine (Gee) - evaluate réalise le téléchargement en un clic de toutes les images individuelles dans la zone d'étude (certaines parties de Shanghai)
Netease's open source distributed storage system curve officially became the CNCF sandbox project
Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance
Writing wechat applet with uni app
炒股票开户的话,手机开户安全吗?有谁知道啊?
数据库系列:MySQL索引优化总结(综合版)
A difficult mathematical problem baffles two mathematicians
Daily 3 questions (3) - check whether integers and their multiples exist
Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
Démarrer avec Apache shenyu