301 Remove Invalid Parentheses 删除无效的括号
删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果。
注意: 输入可能包含了除 ( 和 ) 以外的元素。
示例 :
"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]
详见:https://leetcode.com/problems/remove-invalid-parentheses/description/
方法一:
class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
vector<string> ans;
helperDFS(s, ')', 0,ans);
return ans;
}
void helperDFS(string s, char ch, int last,vector<string> &ans)
{
for(int i = 0, cnt = 0; i < s.size(); i++)
{
if(s[i]=='('||s[i]==')')
{
s[i]==ch?cnt++:cnt--;
}
if(cnt <= 0)
{
continue;
}
for(int j = last; j <= i; j++)
{
if(s[j] == ch && (j ==last || s[j-1]!= ch))
{
helperDFS(s.substr(0, j)+s.substr(j+1), ch, j,ans);
}
}
return;
}
reverse(s.begin(), s.end());
if(ch == ')')
{
return helperDFS(s, '(', 0,ans);
}
ans.push_back(s);
}
};
方法二:
class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
vector<string> res;
unordered_set<string> visited{{s}};
queue<string> q{{s}};
bool found = false;
while (!q.empty())
{
string t = q.front();
q.pop();
if (isValid(t))
{
res.push_back(t);
found = true;
}
if (found)
{
continue;
}
for (int i = 0; i < t.size(); ++i)
{
if (t[i] != '(' && t[i] != ')')
{
continue;
}
string str = t.substr(0, i) + t.substr(i + 1);
if (!visited.count(str))
{
q.push(str);
visited.insert(str);
}
}
}
return res;
}
bool isValid(string t) {
int cnt = 0;
for (int i = 0; i < t.size(); ++i)
{
if (t[i] == '(')
{
++cnt;
}
else if (t[i] == ')' && --cnt < 0)
{
return false;
}
}
return cnt == 0;
}
};
参考:https://blog.csdn.net/qq508618087/article/details/50408894
https://www.cnblogs.com/grandyang/p/4944875.html
301 Remove Invalid Parentheses 删除无效的括号的更多相关文章
- [leetcode]301. Remove Invalid Parentheses 去除无效括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [LeetCode] 301. Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses去除不符合匹配规则的括号
[抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses
题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...
- LeetCode 301. Remove Invalid Parentheses
原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...
- 【leetcode】301. Remove Invalid Parentheses
题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...
- Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)
Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...
- Leetcode 301.删除无效的括号
删除无效的括号 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明: 输入可能包含了除 ( 和 ) 以外的字符. 示例 1: 输入: "()())()" 输出 ...
随机推荐
- hdu - 2066 一个人的旅行(基础最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=2066 把与草儿相连的城市最短距离置为0,然后进行dijkstra,在t个城市里找出距离最近的一个即可. #inc ...
- [bzoj1059][ZJOI2007]矩阵游戏_二分图最大匹配
矩阵游戏 bzoj-1059 ZJOI-2007 题目大意:给定一个n*n的棋盘,上面有一些格子被染黑,剩下都是白色.你每次可以交换两列或者两行,问你能否通过一系列操作使得棋盘的主对角线上的格子全是黑 ...
- pycharm支持react
安装nodejs插件 使能node 出现下面的变化,在scope里可以定义使用的范围 创建react项目 使能eslint规则检查功能 配置前端启动脚本: https://www.jetbrains. ...
- 合并小图片利器TexturePacker GUI
合并小图片,能够非常大的节省网络开销.尤其如今的站点非常喜欢使用大量的小图标来做一些友好提示.当然使用图片文字也是一种选择. 只是这里推荐的是TexturePacker GUI ,这个确实是一款利器. ...
- 【c语言】统计一个数字在排序数组中出现的次数
// 题目:统计一个数字在排序数组中出现的次数. // 比如:排序数组{1.2,3,3,3,3,4.5}和数字3,因为3出现了4次.因此输出4 有一种最简单的算法,遍历.可是有比它效率更高的 先看遍 ...
- cocos2d-x 2.2.3 建project
2.2以后不再使用模板安装了. 打开终端,进入cocos2d-x文件夹下的tools/project-creator,运行命令 ./create_project.py -project [项目名] - ...
- 仰视源代码,实现strcmp
//这是系统库的实现 int strcmp(const char* src, const char* dest) { int rtn = 0; while(!(rtn = *(unsigned cha ...
- 【bzoj1042】[HAOI2008]硬币购物
首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案. 为了避免重复的方案被转移,所以我们以硬币种类为第一层循环,这样阶段性的增加硬币. 一定要注意这个第一层循环要 ...
- 【bzoj1034】[ZJOI2008]泡泡堂BNB
贪心 将双方的选手均按从强到弱排序,然后第一次扫描尽可能用当前剩下的选手中能赢对手当前最强选手中最弱的一个去赢得胜利,若无法做到,则暂时不考虑给对方最强的选手匹配对手.第二遍扫描使用同样策略去获取尽量 ...
- sql 查询如何将结果集 输出为一段字符串?
文件id集合 文件表. SELECT CONCAT('2323',(SELECT 'dsfsd'),'232323'); SELECT CONCAT('2323',(SELECT file_ids F ...