当前位置:网站首页>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;
}
};
边栏推荐
- Apache ShenYu 入門
- Comparable的使用(用于Arrays.sort)
- Some assembly instructions specific to arm64
- Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
- A random number generator
- Daily Mathematics Series 52: February 20
- 16 种企业架构策略
- Jincang KFS data centralized scenario (many to one) deployment
- relu与sigmod的比较
- Crawler scheduling framework of scratch+scratch+grammar
猜你喜欢

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

中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析

Jincang KFS data centralized scenario (many to one) deployment

Technical practice and development trend of video conference all in one machine

CSRF attack

COSCon'22 讲师征集令

Previous string inversion topic

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

Dell technology performs the "fast" formula and plays ci/cd

Getting started with Apache Shenyu
随机推荐
Comparator (for arrays.sort)
Démarrer avec Apache shenyu
Software testing to avoid being dismissed during the probation period
Getting started with Apache Shenyu
Upload and modify the use of avatars
Writing wechat applet with uni app
Technical practice and development trend of video conference all in one machine
网易开源的分布式存储系统 Curve 正式成为 CNCF 沙箱项目
金仓数据库 KingbaseES 插件force_view
ARM64特有一些的汇编指令
Application of global route guard
Advanced single chip microcomputer -- development of PCB (2)
Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
Introduction to socket UDP and TCP
Detection and analysis of simulator in an app
Is it safe for Guosen Securities to open a securities account
MySQL and Oracle processing CLOB and blob fields
GaussDB 如何统计用户sql的响应时间
Comparison between relu and SIGMOD
Jincang database kingbasees plug-in identity_ pwdexp