[LeetCode] Remove Invalid Parentheses
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的更多相关文章
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)
Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...
- [LeetCode] 301. Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- LeetCode 301. Remove Invalid Parentheses
原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...
- [leetcode]301. Remove Invalid Parentheses 去除无效括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses
题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...
- [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- Remove Invalid Parentheses 解答
Question Remove the minimum number of invalid parentheses in order to make the input string valid. R ...
随机推荐
- 如何把excel数据导入数据库
这里介绍2种把excel数据导入oracle数据库的方法. 1. 在excel中生成sql语句. 1)在数据列的右侧,第一行的任何位置输入="insert into table(xx,yyy ...
- HTTP权威指南阅读笔记一:HTTP概述
HTTP协议版本: 1.HTTP/0.9:HTTP的1991原型版本称为HTTP/0.9.这个协议有很多严重的缺陷,只应该用与与老客户端的交互.HTTP/0.9只支持GET方法,不支持多媒体内容的MI ...
- SONATYPE NEXUS搭建MAVEN私服
1.为什么使用Nexus如果没有私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的Maven仓库下载到本地,而一个团队中的所有人都重复的从maven仓库下载构件无疑加大了仓库的负载和浪费 ...
- Git可视化极简易教程 —— Git GUI使用方法
前言 之前一直想一篇这样的东西,因为最初接触时,我也认真看了廖雪峰的教程,但是似乎我觉得讲得有点多,而且还是会给我带来很多多余且重复的操作负担,所以我希望能压缩一下它在我工作中的成本,但是搜索了一下并 ...
- SQL问题集锦
1.union和union all的区别:http://www.cnblogs.com/xiangshu/articles/2054447.html
- dom4j 读取xml
package core.util; import java.io.File; import java.util.Iterator; import java.util.List; import org ...
- Speech两种使用方法
COM组件使用speech: public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVo ...
- WindowsPhone-GameBoy模拟器开发六--[转]指令系统实现必读:补码
网上有同行写了些好文章,在此就不现丑了,贴上连接,放在这里为了补充系列的完整性 计算机为什么选用二进制补码 为什么补码重要?因为计算机中内存.寄存器里面存的数都是用补码表示的!
- Chrome谷歌浏览器首页被改为Hao123导航怎么办|附各类解决方法【转】
软件小子:昨天偶然间发现自己的chrome浏览器的首页被篡改成hao123导航了,要是自己设置的还无所谓,但是后面还有尾巴.顿时就火了,又是哪款软件这么流氓,太无良了,我非常确定我肯定是没有勾选什么设 ...
- assertThat用法
一般匹配符1.assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) ); 注释: allOf匹配符表明如果接下来的所有条件必须 ...