当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

NuxtJS实战案例

Jincang KFS data centralized scenario (many to one) deployment

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

Software testing to avoid being dismissed during the probation period

FPGA基于VGA显示字符及图片

Démarrer avec Apache shenyu

基於Minifilter框架的雙緩沖透明加解密驅動 課程論文+項目源碼

金仓KFS数据级联场景部署

Upload and modify the use of avatars

Explanation and use of kotlin syntax for Android
随机推荐
[shangyun boutique] energy saving and efficiency improvement! Accelerating the transformation of "intelligent manufacturing" in the textile industry
zabbix分布式系统监控
Android:kotlin中Gson与JSON的泛型映射解析
Server rendering
Comparator(用于Arrays.sort)
try-catch-finally
Big Endian 和 Little Endian
10.1. Oracle constraint deferred, not deferred, initially deferred and initially deferred
Crawler scheduling framework of scratch+scratch+grammar
Introduction to socket UDP and TCP
金仓数据库 KingbaseES 插件DBMS_RANDOM
软件测试 避免“试用期被辞退“指南,看这一篇就够了
某APP中模拟器检测分析
scrapy+scrapyd+gerapy 爬虫调度框架
Advanced single chip microcomputer -- development of PCB (2)
Oracle彻底卸载的完整步骤
中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析
Jincang database kingbasees plug-in force_ view
GaussDB 如何统计用户sql的响应时间
报名开启|飞桨黑客马拉松第三期如约而至,久等啦