[Leetcode] 301. Remove Invalid Parentheses 解题报告
来源:互联网 发布:js字符串相等 编辑:程序博客网 时间:2024/06/05 04:19
题目:
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses (
and )
.
Examples:
"()())()" -> ["()()()", "(())()"]"(a)())()" -> ["(a)()()", "(a())()"]")(" -> [""]
思路:
合法的括号串必须具备两个条件:1)左括号的总数等于右括号的总数;2)截止任意一个位置,左括号的数量都必须大于等于右括号的数量。所以我们的思路就是减去最少的括号,使得括号串满足上述两个条件。
对于一个字符串,在任何时候如果')'的个数多余‘(’,则说明从开始到现在为止必然可以删除一个‘)’。而这段子串可能包含多个')',那么删除哪一个呢?其实删除哪一个都可以的。例如对于()())(),从开头到s[4]位置构成的子串多了一个右括号,因此我们需要删掉一个,而这个子串有三个右括号,但是只会产生2个结果,也就是会有一个重复值.所以在删除括号的时候,为保证不会产生重复值,需要记录一个最后删除的位置,这样可以使得在接下来删除的时候只删除这个位置之后的值.这样我们可以使得当前这一段子串不再包含多余的右括号了.这样我们可以删除了一个右括号之后合法的子串与后面还没有检查过的子串组成一个新的字符串重新开始检查.直到不再含有非法的右括号.这样就可以保证满足上段中提到的条件2)了。
但是还有一种情况是包含了多余的左括号,也就是上上段中提到的条件1)。一种直观的方法是从右向左再按照上面的方法处理一遍左括号.但是将数组逆置之后就可以重用上面的算法了。
所以总的思路就是:1)对字符串进行处理使得其不再含有非法右括号;2)将其翻转以后再检查是否含有非法的左括号。最后左右括号都检查完之后都合法就是我们要的答案了。
代码:
class Solution {public: vector<string> removeInvalidParentheses(string s) { vector<string> ret; DFS(ret, s, ')', 0); // determine if ')' need to be removed return ret; }private: void DFS(vector<string> &ret, string s, char ch, int last) { for (int i = 0, cnt = 0; i < s.size(); ++i) { if (s[i] == '(' || s[i] == ')') { s[i] == ch ? ++cnt : --cnt; } if (cnt <= 0) { // valid up to now continue; } for (int j = last; j <= i; ++j) { if (s[j] == ch && (j == last || s[j - 1] != ch)) { // try to remove ch at s[j] DFS(ret, s.substr(0, j) + s.substr(j + 1), ch, j); } } return; } reverse(s.begin(), s.end()); if (ch == ')') { // check if '(' need to be removed DFS(ret, s, '(', 0); } else { ret.push_back(s); } }};
- [leetcode] 301. Remove Invalid Parentheses 解题报告
- [Leetcode] 301. Remove Invalid Parentheses 解题报告
- Leetcode 301. Remove Invalid Parentheses
- leetcode 301. Remove Invalid Parentheses
- [leetcode] 301. Remove Invalid Parentheses
- leetcode.301. Remove Invalid Parentheses
- [LeetCode]--301. Remove Invalid Parentheses
- Leetcode 301. Remove Invalid Parentheses
- 【LeetCode】301. Remove Invalid Parentheses
- [LeetCode]301. Remove Invalid Parentheses
- Leetcode 301. Remove Invalid Parentheses
- leetcode 301. Remove Invalid Parentheses
- [leetcode]301. Remove Invalid Parentheses
- Leetcode: 301.Remove Invalid Parentheses
- [LeetCode] Remove Invalid Parentheses
- LeetCode -- Remove Invalid Parentheses
- leetcode Remove Invalid Parentheses
- LeetCode Remove Invalid Parentheses
- Oracle PL/SQL的存储过程
- react-native笔记
- Boost Thread使用
- 关于Java的File.separator
- poj3311 动态规划 轮廓线 或者状态压缩
- [Leetcode] 301. Remove Invalid Parentheses 解题报告
- 在vue中使用sass的配置的方法
- 微软100题-009
- Linux快捷键总结
- Java中的static关键字解析
- Android--------在Fragment中通过MVP模式用Okhttp进行数据请求
- 有关java web工程降级jdk遇到的问题
- 归并排序和快速排序
- JavaScript-DOM(下篇)