[leetcode] 301. Remove Invalid Parentheses
来源:互联网 发布:电脑定闹钟软件 编辑:程序博客网 时间:2024/05/17 06:43
题目:
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())()”]
“)(” -> [“”]
题意:
给了一个表达式,里面有左右括号以及其他的标识符,现在要删除最少的括号,使左右括号变成有效的。有效的意思就是一个右括号出现时前面必然有且只有一个左括号与之配对。
思路:
首先,我们要保证删除的左括号或者右括号的数目最少,并且要保证括号必须满足一个右括号之前必须有一个左括号与之配对。那么我们可以扫描一遍,从而确定多余的左括号数目以及多余的右括号数目,我们需要清楚一点左括号和右括号都可能有多余。怎么扫描来确定这两个多余的数目呢,每当我们扫描到左括号时让多余的左括号数加一,每当扫描到右括号时,如果还有多余的左括号,那么就让多余的左括号数目减一,相当于配对这个扫描到右括号,如果已经没有左括号来匹配这个右括号,那么让多余的右括号数目加一(这个是最终要删除的右括号的数目)。这样子扫描一遍之后就可以确定需要删除的左括号以及右括号的数目。
其次,我们采取回溯的方法,来扫描字符串,因为刚才已经计算出了需要删除的左括号数目以及右括号的数目,那么再回溯过程中,我们可以利用这个条件进行剪枝,保证删除的左括号数目不超过刚才计算的左括号数目,保证删除的右括号数目不超过计算得到的需要删除的右括号的数目。
但是还需要满足的一个条件是保证左右括号配对,也就是说每个右括号之前必须要有一个左括号,我们也用一个数字来记录,如果把左括号保存到最终结果中,那么就让这个计数加一,如果把右括号保存进字符串,那么就让这个计数减一,必须保证这个计数非负。
代码如下:
class Solution {public: vector<string> removeInvalidParentheses(string s) { int left = 0, right = 0; if(s.empty())return vector<string>(1, ""); vector<string> result; for(auto c:s) { switch(c) { case '(':left++;break; case ')':left>0?left--:right++;break; default:break; } } backTracking(s, 0, left, right, 0, "", result); return result; } void backTracking(string& s, int index, int left, int right, int leftMinusRight, string temp, vector<string>& result) { if(index == s.length() && left == 0 && right == 0 && leftMinusRight == 0) { if(dict.find(temp) == dict.end()) { result.push_back(temp); dict.insert(temp); } } if(index == s.length() || left < 0 || right < 0 || leftMinusRight < 0)return; char c = s[index]; if(c == '(') { backTracking(s, index + 1, left - 1, right, leftMinusRight, temp, result); temp.push_back('('); backTracking(s, index + 1, left, right, leftMinusRight + 1, temp, result); } else if(c == ')') { backTracking(s, index + 1, left, right - 1, leftMinusRight, temp, result); temp.push_back(')'); backTracking(s, index + 1, left, right, leftMinusRight - 1, temp, result); } else { temp.push_back(c); backTracking(s, index + 1, left, right, leftMinusRight, temp, result); } }private: unordered_set<string> dict;};
- 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
- [Leetcode]Remove Invalid Parentheses
- leetcode:Remove Invalid Parentheses
- XSS过滤器Bypass的一些姿势 - xiaix
- 自定义简单实现滑动下拉刷新效果
- iOS面试知识点
- UITextField
- ALIGN 边界对齐指令
- [leetcode] 301. Remove Invalid Parentheses
- linux防火墙的设置
- Android 动画详解
- NSTimer用法,暂停,继续,初始化
- 群发图文素材(测试号操作及出现的问题)
- Core Data入门使用
- android自定义钟摆loadingView
- Trie树:应用于统计和排序
- Linux下面批量删除某种类型的文件