[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 ...
随机推荐
- TypeScript:类(Classes)
返回TypeScript手册总目录 传统的Javascript关注的是函数(function)和基于原型(prototype-based)的继承作为构建可重复使用组件的基本方式,但是与更舒服地使用面向 ...
- 分享我用Qt开发的应用程序【一】,附绿色版下载,以后会慢慢公布源码
写在前面: 1.第一版的代码还有些烂,等功能开发齐全了,做一次重构,再慢慢分享代码 2.邮箱功能.自动升级功能还没有做,笔记功能和备忘功能是好用的,大家如果不嫌弃,可以先用起来 3.笔记功能目前还不能 ...
- [ucgui] 对话框2——小窗口初始化与消息响应
>_<" 上一节已经说过,创建过得窗口虽然可见,但是它们是以 “空”的形式出现的.这是因为对话框过程函数尚未包含初始化单个元素的代码.小工具的初始值.由它们所引起的行为以及它们之 ...
- 安全性之DDOS的防护技巧
网站的信息安全越来越重要,结合自己1年多的互联网金融方面的安全防护做些总结. 后续希望研究并运用: 1.加密算法 2.DDOS的防护技巧 3.跨站点请求伪造 4.XSS攻击 5.文件上传漏洞 6.信息 ...
- crossplatform---Nodejs in Visual Studio Code 06.新建Module
1.开始 Node.js:https://nodejs.org 2.Moudle js编程中,由于大家可以直接在全局作用域中编写代码,使开发人员可以很容易的新建一个全局变量或这全局模块,这些全局变量或 ...
- ios之VFL的补充(二)
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[languageI ...
- (转)【Unity3d】Glow + Outline 轮廓描边
转:http://www.cnblogs.com/dosomething/archive/2012/08/04/2622488.html [Unity3d]Glow + Outline 轮廓描边 轮廓 ...
- HTTP请求报文和HTTP响应报文(转)
原文地址:http://blog.csdn.net/zhangliang_571/article/details/23508953 HTTP报文是面向文本的,报文中的每一个字段都是一些ASCII码串, ...
- java 调用 .net webservice 示例
String url="http://IP:端口/LisService.asmx"; String methodName="GetLisResultForBlood&qu ...
- 【C++沉思录】句柄1
1.在[C++沉思录]代理类中,使用了代理类,存在问题: a.代理复制,每次创建一个副本,这个开销有可能很大 b.有些对象不能轻易创建副本,比如文件2.怎么解决这个问题? 使用引用计数句柄,对动态资源 ...