LintCode-780: Remove Invalid Parentheses (DFS 难题)

这题不容易。可以用DFS做,也可以用BFS做。

解法1:
我参考了网上的solution,不过感觉这种方法好像不一定是最优。

class Solution {
public:
    /**
     * @param s: The input string
     * @return: Return all possible results
     */
    vector<string> removeInvalidParentheses(string &s) {
        string sol;
        vector<string> results;
        
        if (s.empty()) {
            results.push_back("");
            return results;
        }
        
        int l_more = 0;   // shows # of extra '('
        int r_more = 0;   // shows # of extra ')'
    
        for (int i = 0; i < s.size(); ++i) {
    
            if (s[i] == '(') {
                l_more++;
            }  
    
            if (s[i] == ')') {
                if (l_more > 0)
                    l_more--;
                else
                    r_more++;
            }
        }
        
        helper(s, 0, l_more, r_more, results);
        
        return results;
    }
    
private:
    bool isVald(string &s) {
        int count = 0;
        for (auto c : s) {
            if (c == '(') count++;
            if (c == ')') {
                count--;
                if (count < 0) return false;
            }
        }
        
        return (count == 0);
    }
    
    void helper(string &s, int index, int l_more, int r_more, vector<string> & results) {
        
        if (l_more == 0 && r_more == 0) {
            if (isVald(s)) {
                results.push_back(s);
                return;
            }
        }
        
        for (int i = index; i < s.size(); ++i) {
            if (i != index && s[i] == s[i-1])    //去重, if ))) or (((, just remove the last one)
                continue;
            
            if (s[i] == '(' && l_more > 0) {
                string new_str = s.substr(0, i) + s.substr(i + 1);    
                  //if index==0, substr(0,index) = ""
                helper(new_str, i, l_more - 1, r_more, results);  //note here is still i !!!
            }
            
            if (s[i] == ')' && r_more > 0) {
                string new_str = s.substr(0, i) + s.substr(i + 1);
                helper(new_str, i, l_more, r_more - 1, results);
            }
        }
    }
    
};

注意:
1)因为每次生成新str,所以就不需要再results.pop_back()。
2)注意生成新str后, helper()里面还是i,不是i+1,因为这个新str要重新检查。
3) 注意去重。
4) 注意isValid()的写法,比较巧妙。
5) 注意生成新string的写法
string new_str = s.substr(0, i) + s.substr(i + 1);
//if index==0, substr(0,index) = “”

解法2:还是DFS
https://www.jiuzhang.com/solution/remove-invalid-parentheses/#tag-other-lang-java
zxqiu的做法好像很巧妙。没时间细看。
下次做。

解法3: BFS

class Solution {
public:
    vector<string> removeInvalidParentheses(string s) {
        vector<string> res;
        queue<string> q;
        set<string> strSet;
        q.push(s);
        strSet.insert(s);
        int step = 0, minStep = INT_MAX;
        while(!q.empty()) {
            int qSize = q.size();
            for (int i = 0; i < qSize; i++) {
                string frontStr = q.front();
                q.pop();
                if (isValidStr(frontStr)) {
                    if (step <= minStep) {
                        res.push_back(frontStr);
                        minStep = min(step, minStep);
                    }
                    continue; //要不要都可以
                }

                for (int i = 0; i < frontStr.size(); i++) {
                    string tmpStr = frontStr.substr(0, i) + frontStr.substr(i + 1);
                    if (strSet.find(tmpStr) == strSet.end()) {
                        q.push(tmpStr);
                        strSet.insert(tmpStr);
                    }
                }
            }
            step++;
        }
        return res;
    }
private:
    bool isValidStr(string s) {
        int n = s.size();
        int count = 0;
        stack<char> stk;
        for (auto c : s) {
            if (c == '(') {
                stk.push(c);
            } else if (c == ')') {
                if (!stk.empty() && stk.top() == '(') stk.pop();
                else return false;
            }
        }
        return stk.empty();
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值