当前位置:网站首页>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;
}
};
边栏推荐
- FPGA displays characters and pictures based on VGA
- Explanation and use of kotlin syntax for Android
- Writing wechat applet with uni app
- Oracle彻底卸载的完整步骤
- 3 Questions par jour (3) - vérifier l'existence d'entiers et de leurs doubles
- Shen Ying, China Academy of communications and communications: font open source protocol -- Introduction to ofl v1.1 and analysis of key points of compliance
- Multiple environment variables
- Five types of questions about network planning
- relu与sigmod的比较
- A program reflecting the characteristics of C language program structure
猜你喜欢
Socket communication principle
视频会议一体机的技术实践和发展趋势
FPGA基于VGA显示字符及图片
Démarrer avec Apache shenyu
Apache ShenYu 入門
Netease's open source distributed storage system curve officially became the CNCF sandbox project
看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-7
Oracle彻底卸载的完整步骤
Apache ShenYu 入门
C disk uses 100% cleaning method
随机推荐
Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
报名开启|飞桨黑客马拉松第三期如约而至,久等啦
Netease's open source distributed storage system curve officially became the CNCF sandbox project
Writing wechat applet with uni app
FPGA displays characters and pictures based on VGA
wait()、notify()和notifyAll()、sleep()、Condition、await()、signal()
Getting started with Apache Shenyu
Use of three-level linkage plug-ins selected by provinces and cities
Crawler scheduling framework of scratch+scratch+grammar
Previous string inversion topic
中国信通院沈滢:字体开源协议——OFL V1.1介绍及合规要点分析
撸一个随机数生成器
每日3題(3)-檢查整數及其兩倍數是否存在
At 16:00 today, Mr. sunxiaoming, a researcher of the Institute of computing, Chinese Academy of Sciences, took you into the quantum world
COSCon'22 讲师征集令
Introduction to socket UDP and TCP
Nuxtjs actual combat case
How to start the phpstudy server
Bayes
Technical practice and development trend of video conference all in one machine