删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果。
注意: 输入可能包含了除 ( 和 ) 以外的元素。
示例 :
"()())()" -> ["()()()", "(())()"]
"(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 删除无效的括号的更多相关文章

  1. [leetcode]301. Remove Invalid Parentheses 去除无效括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  2. [LeetCode] 301. Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  3. 301. Remove Invalid Parentheses去除不符合匹配规则的括号

    [抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...

  4. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  5. 301. Remove Invalid Parentheses

    题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...

  6. LeetCode 301. Remove Invalid Parentheses

    原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...

  7. 【leetcode】301. Remove Invalid Parentheses

    题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...

  8. Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)

    Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...

  9. Leetcode 301.删除无效的括号

    删除无效的括号 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明: 输入可能包含了除 ( 和 ) 以外的字符. 示例 1: 输入: "()())()" 输出 ...

随机推荐

  1. python dos2unix

    有时你在windows上创建的文件拿到Linux/unix上运行会出错, 这是因为windows上有些字符如换行符在linux/unix不识别.这种情况下需要用dos2unix这个工具把文件转换成li ...

  2. symfony could not load type 'datetime'

    当用curd生成控制器后,当修改的时候,会有这个提示,解决方法 在orm中通过事务的方式填充时间,然后把生成的form中的文件的时间段去掉 $builder ->add('title') -&g ...

  3. win7开启超级管理员账户(Administrator)

    win7开启超级管理员账户(Administrator) 不同于XP系统,Windows7系统据说出于安全的考虑,将超级管理员帐户"Administrator"在登陆界面给隐藏了, ...

  4. python模块之 paramiko(转载)

    paramiko模块提供了ssh及sft进行远程登录服务器执行命令和上传下载文件的功能.这是一个第三方的软件包,使用之前需要安装. 1 基于用户名和密码的 sshclient 方式登录 # 建立一个s ...

  5. ramfs、rootfs和initramfs【转】

    ramfs, rootfs and initramfs October 17, 2005 Rob Landley <rob@landley.net> =================== ...

  6. hibernate 下载

    https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/ http://sourceforge.net/p ...

  7. [RK3288][Android6.0] 调试笔记 --- 如何确认声卡是否注册成功【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/78399875 Platform: RK3288 OS: Android 6.0 Kernel ...

  8. POJ 2080:Calendar

    Calendar Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12546   Accepted: 4547 Descrip ...

  9. bzoj1999 (洛谷1099) 树网的核——dfs

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1999  https://www.luogu.org/problemnew/show/P109 ...

  10. vue-easytable

    github地址:https://github.com/huangshuwei/vue-easytable