This problem can be solved very elegantly using BFS, as in this post.

The code is rewritten below in C++.

 class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
vector<string> parens;
queue<string> candidates;
unordered_set<string> visited;
candidates.push(s);
visited.insert(s);
bool found = false;
while (!candidates.empty()) {
string now = candidates.front();
candidates.pop();
if (isValid(now)) {
parens.push_back(now);
found = true;
}
if (!found) {
int n = now.length();
for (int i = ; i < n; i++) {
if (now[i] == '(' || now[i] == ')') {
string next = now.substr(, i) + now.substr(i + );
if (visited.find(next) == visited.end()) {
candidates.push(next);
visited.insert(next);
}
}
}
}
}
return parens;
}
private:
bool isValid(string& s) {
int c = , n = s.length();
for (int i = ; i < n; i++) {
if (s[i] == '(') c++;
if (s[i] == ')' && !c--) return false;
}
return !c;
}
};

[LeetCode] Remove Invalid Parentheses的更多相关文章

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

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

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

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

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

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

  4. LeetCode 301. Remove Invalid Parentheses

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

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

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

  6. 301. Remove Invalid Parentheses

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

  7. [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses

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

  8. Remove Invalid Parentheses

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

  9. Remove Invalid Parentheses 解答

    Question Remove the minimum number of invalid parentheses in order to make the input string valid. R ...

随机推荐

  1. [MSSQL2012]LEAD函数

    LEAD函数简单点说,就是把下一行的某列数据提取到当前行来显示,看示例更能解释清楚,先看测试用脚本 DECLARE @TestData TABLE(     ID INT IDENTITY(1,1), ...

  2. 第八章xml学习

    1.ASP.NET和JSP的关系 ASP.NET 和JSP都是用来开发动态网站的技术,只不过ASP.NET是通过c#语言来操作的, 而JSP是通过Java语言来操作的. 2.为什么学习XML? 01. ...

  3. C#与数据库访问技术总结(五)之Command对象的常用方法

    Command对象的常用方法 说明:上篇总结了Command对象的几个数据成员,这节总结Command对象的常用方法. 同样,在不同的数据提供者的内部,Command对象的名称是不同的,在SQL Se ...

  4. APU平台DirectX 12性能测试:超级大惊喜!

    APU平台DirectX 12性能测试:超级大惊喜! 转自:http://www.ithome.com/html/digi/129840.htm [size=1pc]微软将会在接下来的GDC 2015 ...

  5. atitit.二进制数据无损转字符串网络传输

    atitit.二进制数据无损转字符串网络传输 1. gbk的网络传输问题,为什么gbk不能使用来传输二进制数据 1 2. base64 2 3. iso-8859-1  (推荐) 2 4. utf-8 ...

  6. Spark Scala 读取GBK文件的方法

    1. 在生产环境下,很多文件是GBK编码格式的,而SPARK 常用的textFile方法默认是写死了读UTF-8格式的文件,其他格式文件会显示乱码 用如下代码实现读取GBK文件的方法 import o ...

  7. D. Book of Evil

    D. Book of Evil time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. JavaScript简介及基础知识(1)

    1.JavaScript是什么—它是个脚本语言,需要宿主文件,它的宿主文件是html文件. Javascript是一种脚本语言,比HTML要复杂.不过即便你先前不懂编程,也不用担心,因为Javascr ...

  9. 菜鸟日记-HTML

    第一部分  HTML---Hyper Text Markup Language--超文本标记语言 1.HTML标准:<html> <head> 网页上的控制信息 <tit ...

  10. iOS开发---集成百度地图完善版

    一.成为百度的开发者.创建应用 http://developer.baidu.com/map/index.php?title=首页 (鼠标移向 然后选择你的项目需要的功能 你可以在里面了解到你想要使用 ...