当前位置:网站首页>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;
}
};
边栏推荐
- How to start the phpstudy server
- Kingbasees plug-in DBMS of Jincang database_ session
- 龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书
- Course paper + code and executable EXE file of library information management system based on C language
- Arrays.asList()
- Jincang KFS data centralized scenario (many to one) deployment
- 反应c语言程序结构特点的程序
- 金仓数据库 KingbaseES 插件DBMS_RANDOM
- 金仓KFS数据级联场景部署
- Network remote access using raspberry pie
猜你喜欢

Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files

Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
![[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology](/img/82/8cac87231e51698ab17f1274b3a0bd.jpg)
[observation] objectscale: redefining the next generation of object storage, reconstruction and innovation of Dell Technology

1-7snapshots and clones in VMWare

基于Minifilter框架的双缓冲透明加解密驱动 课程论文+项目源码

Android之Kotlin语法详解与使用

龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书

Coscon'22 lecturer solicitation order

Oracle彻底卸载的完整步骤

c盘使用100%清理方法
随机推荐
Open source invites you to participate in the openssf Open Source Security Online Seminar
查询法,中断法实现USART通信
[维护集群案例集] GaussDB 查询用户空间使用情况
Nuxtjs actual combat case
基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件
Is it safe to open a securities account in changtou school by mobile phone?
Comparator (for arrays.sort)
GaussDB 如何统计用户sql的响应时间
Continuous delivery jenkinsfile syntax
今天16:00 | 中科院计算所研究员孙晓明老师带大家走进量子的世界
Getting started with Apache Shenyu
Checking whether the double value is an integer - Swift - checking if a double value is an integer - swift
每日3題(3)-檢查整數及其兩倍數是否存在
金仓KFS数据集中场景(多对一)部署
Kingbasees plug-in DBMS of Jincang database_ OUTPUT
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
What are the functions of arm64 assembly that need attention?
GC
Some assembly instructions specific to arm64
网络远程访问的方式使用树莓派