删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果。
注意: 输入可能包含了除 ( 和 ) 以外的元素。
示例 :
"()())()" -> ["()()()", "(())()"]
"(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. 解决Android Studio2.0不能使用HttpClient

    在build.gradle中的android {}中加上useLibrary 'org.apache.http.legacy'

  2. 调用系统相机拍照,保存照片,调用系统裁剪API对照片处理,显示裁剪之后的照片

    package com.pingyijinren.test; import android.annotation.TargetApi; import android.app.Notification; ...

  3. request详究

    本文主要是对在学习过程中遇到的request用法进行归纳总结,彻底的搞明白request在jsp中的作用. 百度百科的介绍如下: Request对象的作用是与客户端交互,收集客户端的Form.Cook ...

  4. 【天道酬勤】 腾讯、百度、网易游戏、华为Offer及笔经面经(转)

    应届生上泡了两年,一直都是下资料,下笔试题,面试题.一直都在感谢那些默默付出的人.写这个帖子花了我两 个夜晚的时间,不是为了炫耀,只是为了能给那些“迷惘”的学弟学妹,一点点建议而已.大家何必那么认真, ...

  5. 动态演示冒泡排序java

    动态演示冒泡排序java //冒泡排序是一种简单的交换排序,基本思路,从数列左边开始扫描元素,在扫描过程中依次对相邻元素进行比较,将较大元素后移. public class NumberSort { ...

  6. 二 hbase

    Hbase 本文介绍Hbase.但本文的前提是假设你已经读过Google的BigTable论文. Introduction Hbase 是基于Google Big Table用java实现的分布式,列 ...

  7. windows 7 忘記密碼,用“带命令行的安全模式”

    net user administrator /active:yes net user tester /add net localgroup administrators tester /add

  8. VC,VB程序button、图标样式美化

    此处的"美化"指的不是通过代码进行美化你的程序.关于想进一步优化自己的程序界面的,最好还是去了解下SkinSharp吧.本文提及的是利用第三方资源编辑软件在不更改程序不论什么框架和 ...

  9. 创建一个Cordova完整应用

    本文承接上篇<创建Cordova插件>,通过实现一个简单的应用作为这个Cordova0基础系列的结束. 前边对Cordova编程已经讲了不少了.还没有拿真实应用为例完整的演练一遍构建过程. ...

  10. MFC自己主动获取网络地址函数实现----广播地址,网关,子网掩码

    void CSetSignalBoxDlg::OnBnClickedButtonGetbroadcastaddr() {       //凝视部分为还有一种获取IP方式,可略过 //char Name ...