Minimum Remove to Make Valid Parentheses(C++移除无效的括号)

解题思路:

(1)从前往后,从后往前,记录balance的值

class Solution {
public:
    string minRemoveToMakeValid(string s) {
        int balance = 0;
        int i=0;
        while(i=0) {
           if(s[i]=='(') {
                balance--;
                if(balance==0) {
                   s.erase(s.begin()+i);
                   return s;
                } else {
                    s.erase(s.begin()+i);
                    i--;
                }
            } else i--;
        }
        
        return s;
    }
};

 

你可能感兴趣的:(C++,LeetCode,c++,leetcode,括号匹配)