当前位置:网站首页>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~~~~

 Insert picture description here
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;
    }
    
};
原网站

版权声明
本文为[I'm not xiaohaiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251055162554.html