当前位置:网站首页>Leetcode 1249. Remove invalid brackets (awesome, finally made)
Leetcode 1249. Remove invalid brackets (awesome, finally made)
2022-06-25 11:17:00 【I'm not xiaohaiwa~~~~】

Here you are ‘(’、‘)’ And a string of lowercase letters s.
You need to remove the minimum number of entries from the string ‘(’ perhaps ‘)’ ( You can remove brackets anywhere ), Make the rest 「 Bracket string 」 It works .
Please return any legal string .
It works 「 Bracket string 」 It should conform to the following Any one of them requirement :
Empty strings or strings containing only lowercase letters
Can be written AB(A Connect B) String , among A and B It's all effective 「 Bracket string 」
Can be written (A) String , among A Is an effective 「 Bracket string 」
Example 1:
Input :s = “lee(to)de)”
Output :“lee(to)de”
explain :“lee(t(co)de)” , “lee(tode)” It's also a possible answer .
Example 2:
Input :s = "a)b(c)d"
Output :"ab(c)d"
Example 3:
Input :s = "))(("
Output :""
explain : Empty strings are also valid
Tips :
- 1 <= s.length <= 10^5
- s[i] May be ‘(’、‘)’ Or English lowercase letters
Main idea : Customize a structure , Used to record ’(‘ and ‘)’ Location information for
Then remove the valid parentheses , What is left is superfluous ’(‘ and ’)', Then iterate through the original string , Skipping these extra parentheses is the answer .
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);// Record bracket information
}
}
if(vec.size()==0)
return s;
// The following steps are to remove valid parentheses
vector<param>vec2;// Illegal bracket information left at the end of this array
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;
}
};
边栏推荐
- 基于Minifilter框架的双缓冲透明加解密驱动 课程论文+项目源码
- Some assembly instructions specific to arm64
- 中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析
- scrapy+scrapyd+gerapy 爬虫调度框架
- Comparable的使用(用于Arrays.sort)
- 2022年PMP项目管理考试敏捷知识点(2)
- Kingbasees plug-in ftutilx of Jincang database
- Arrays.asList()
- Big endian and little endian
- 从GEE中免费获取全球人类住区层 (GHSL) 数据集
猜你喜欢

Task03 probability theory

Double tampon transparent cryptage et décryptage basé sur le cadre minifilter

【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?

Nuxtjs actual combat case

视频会议一体机的技术实践和发展趋势

SystemVerilog (XIII) - enumerate data types

软件测试 避免“试用期被辞退“指南,看这一篇就够了

1-7snapshots and clones in VMWare

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

Ouverture de l'inscription | le troisième marathon des hackers de pagaie est arrivé comme prévu.
随机推荐
Detection and analysis of simulator in an app
今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目
开源社邀请您参加OpenSSF开源安全线上研讨会
A random number generator
每日3题(3)-检查整数及其两倍数是否存在
Comparator(用于Arrays.sort)
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology
Big endian and little endian
Handler、Message、Looper、MessageQueue
每日3題(3)-檢查整數及其兩倍數是否存在
基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
Socket communication principle
SystemVerilog (XIII) - enumerate data types
Jincang database kingbasees plug-in identity_ pwdexp
GaussDB 如何统计用户sql的响应时间
Simple use of SVN
Android: generic mapping analysis of gson and JSON in kotlin
try-catch-finally
Complete steps for a complete Oracle uninstall