删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果。
注意: 输入可能包含了除 ( 和 ) 以外的元素。
示例 :
"()())()" -> ["()()()", "(())()"]
"(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. 使用XML定义组件样式

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content& ...

  2. Delphi与Windows 7下的用户账户控制(UAC)机制

    WIN7, Vista提供的UAC机制,它的主要目的是防止对于操作系统本身的恶意修改.对于Delphi程序的影响,UAC主要在于以下几点:1.由于UAC机制,Delphi对于系统的操作可能无声的失败, ...

  3. 显示锁ReentrantLock和Condition的使用

    一.ReentrantLock (1).java.util.concurrent.locks包中的ReentrantLock就是重入锁,它实现了Lock接口,Lock加锁和解锁都是显示的.Reentr ...

  4. 携程Apollo(阿波罗)配置中心本地开发模式不接入配置中心进行本地开发

    官方教程:https://github.com/ctripcorp/apollo/wiki/Java%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8 ...

  5. Java的文件注释

    以下内容引用自http://wiki.jikexueyuan.com/project/java/documentation.html: Java语言支持三种注释形式: 注释 描述 /*text*/ 编 ...

  6. JSP页面不支持EL表达式的解决方法

    JSP页面不支持EL表达式的问题就出在新建项目时web.xml的声明上. web.xml声明部分一般分为如下版本的xsd: web-app_2_2.xsd web-app_2_3.xsd web-ap ...

  7. OCP知识点讲解 之 队列、资源与锁:RHCA|OCM|CCIE RedHat大中华地区前50位RHCA系统架构师:叶绍琛

      一.队列与共享资源 共享资源可以被多个会话.进程同时访问,因此它的访问需要保护.Oracle中,除了PGA,所有的东西(包括内存.磁盘.CPU.表.索引.事务等等,种类太多,一概用东西两字来代表) ...

  8. dataTables 添加行内操作按钮

    在上一篇博客中我们提到了用dataTables 做一个分页表格.今天进一步进阶,做一个行内带操作按钮的表格.效果如图. 记得最基础的实现方式是,我们要在js 中拼接字符串,嵌入一个带按钮的语句.但是现 ...

  9. Ubuntu14.04 忘记rootpassword的解决方法

    对于windows操作系统,假设忘记管理员password,事实上还是蛮好解决的. 最简单的一种方式就是使用PE--电脑店,老毛桃等等,都能够非常完美的解决问题. 假设是Linux操作系统的话.事实上 ...

  10. echars入门篇

    官网地址:echars. 官方实例:首次使用请点击. 官方文档以及第一次操作实例如下 我们生活中有很多统计图,举例一下,有:柱形图.饼图.折线图等一些可以统计数据的形式. 图有:X轴(横轴),Y轴(纵 ...