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. [转]DOS特殊字符转义方法

    http://www.robvanderwoude.com/escapechars.php 期望得到的字符 转义后字符 说明 % %% May not always be required in do ...

  2. C语言 队列 链式结构 实现

    一个C语言链式结构实现的队列 mQueue (GCC编译). /** * @brief C语言实现的链式队列 * @author wid * @date 2013-10-31 * * @note 若代 ...

  3. eclipse导入Android项目后,项目的名称变为了主Activity的名称

    不要使用File -> Import -> Android -> Existing Android Code into Workspace,而是用 File -> Import ...

  4. HTTP权威指南阅读笔记四:连接管理

    HTTP通信是由TCP/IP承载的,HTTP紧挨着TCP,位于其上层,所以HTTP事务的性能很大程度上取决于底层TCP通道的性能. HTTP事务的时延 如图: HTTP事务的时延有以下几种主要原因. ...

  5. [异常] JLink Error: Could not find supported CPU core on JTAG chain J-Link连接不到stm32内核问题

    >_<" 昨天晚上还好好的,今天早上调试的时候就不行了,下载程序的时候总是报J-Link连接不上,而且stm32似乎也死机了,led灯不闪烁,TFT屏也无显示. >_< ...

  6. apache 虚拟主机的配置

    一.基于IP 1. 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP: [root@localhost root]# ifconfig et ...

  7. iOS系统app崩溃日志手动符号化

    iOS系统app崩溃日志手动符号化步骤: 1.在桌面建立一个crash文件夹,将symbolicatecrash工具..crash文件..dSYM文件放到该文件夹中 a.如何查询symbolicate ...

  8. [数据库操作]Java中的JDBC的使用方法.

    前言:想必大家在实际编码中都遇到过JDBC的操作, 这里仅做自己的一个总结, 有错误和不完整之处还请大家提出来. 1,JDBC其实一套规范(接口)数据库厂商需要实现此接口(实现类)--数据库驱动 2, ...

  9. Atitit. 提升开发效率与质量DSL ( 3) ----实现DSL的方式总结

    Atitit. 提升开发效率与质量DSL ( 3) ----实现DSL的方式总结 1. 管道抽象 1 2. 层次结构抽象(json,xml etc) 1 3. 异步抽象promise 1 4. Ide ...

  10. atitit.提升2--3倍开发效率--cbb体系的建设..

    atitit.提升开发效率--cbb体系的建设.. #--提升倍数,大概2--3倍.. #---cbb的内容 知识的,expt的,经验的技术的部件的问题库的角度.. #---cbb的层次,tech l ...