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

本文介绍了一种使用深度优先搜索(DFS)和广度优先搜索(BFS)解决删除无效括号问题的方法。通过两种方法,可以找到所有可能的有效括号组合。

这题不容易。可以用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();
    }
};
### `-audiodev: invalid option` 错误分析与解决方法 QEMU 的 `-audiodev` 参数用于配置音频后端设备,但若使用的 QEMU 版本不支持该选项,或者命令行参数格式有误,则会提示 `invalid option` 错误。该参数是在 QEMU 4.0 之后引入的,用于替代旧版本中的 `-soundhw` 和其他音频相关参数[^1]。 若在较旧版本的 QEMU 中使用 `-audiodev`,会出现命令行解析错误,提示 `invalid option`。例如,某些 Linux 发行版默认安装的 QEMU 版本可能低于支持该参数的版本(如 Ubuntu 18.04 默认安装 QEMU 2.11),导致无法识别 `-audiodev` 选项[^1]。 为避免该错误,可以使用更早版本的音频配置方式,例如通过 `-soundhw` 参数启用默认音频设备。以下是一个兼容性更强的 QEMU 启动命令示例: ```bash qemu-system-arm -M realview-pbx-a9 -nographic -kernel /path/to/openwrt-realview-vmlinux-initramfs.elf \ -net nic -net bridge,br=br0 -soundhw ac97 ``` 该命令使用 `-soundhw ac97` 启用 AC97 音频设备,适用于大多数旧版本 QEMU 环境[^1]。 如果需要使用 `-audiodev` 参数,则应确保 QEMU 版本不低于 4.0,并使用如下格式指定音频设备: ```bash qemu-system-arm -M realview-pbx-a9 -nographic -kernel /path/to/openwrt-realview-vmlinux-initramfs.elf \ -net nic -net bridge,br=br0 -audiodev pa,id=pa -device ac97,audiodev=pa ``` 上述命令中,`-audiodev pa,id=pa` 指定使用 PulseAudio 作为音频后端,`-device ac97,audiodev=pa` 将 AC97 音频设备绑定到该音频后端[^1]。 若无需音频功能,可直接省略音频相关参数以避免错误,例如: ```bash qemu-system-arm -M realview-pbx-a9 -nographic -kernel /path/to/openwrt-realview-vmlinux-initramfs.elf \ -net nic -net bridge,br=br0 ``` 此配置将禁用音频设备,避免因音频支持导致的兼容性问题[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值