[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 ...
随机推荐
- 你不得不知道的HTML5的新型标签
<article>标签定义外部的内容.比如来自一个外部的新闻提供者的一篇新的文章,或者来自 blog 的文本,或者是来自论坛的文本.亦或是来自其他外部源内容. <aside>标 ...
- [ACM_数学] Fibonacci Nim(另类取石子,2-4组合游戏)
游戏规则: 有一堆个数为n的石子,游戏双方轮流取石子,满足: 1)先手不能在第一次把所有的石子取完: 2)之后每次可以取的石子数介于1到对手刚取的石子数的2倍之间(包含1和对手刚取的石子数的2倍). ...
- JQuery文本框水印插件的简单实现
采用JQuery实现文本框的水印效果非常容易,效果如下: 代码片段,定义要应用水印效果的文本框的样式: .watermark { color: #cccccc; } 将JavaScript代码封装成J ...
- 集合使用copy与mutableCopy的区别
集合(NSArray,NSSet,NSDictionary等)使用copy与mutableCopy的区别是类似的,下面以NSMutableArray.NSArray 为例子验证如下: NSMutabl ...
- 基于.NET的Excel开发:单元格区域的操作(读取、赋值、边框和格式)
引用 using Excel = Microsoft.Office.Interop.Excel; 定义 Excel.ApplicationClass app; Excel.Workbooks book ...
- 【UML】UML基础知识
UML简介 统一建模语言(UML)是一个通用的可视化建模语言,用于对软件进行描述.可视化处理.构造和建立软件系统制品的文档. 它记录了对必须构造的系统的决定和理解,可用于对系统的理解. ...
- 看2016上半年O2O新风向,太阳终会穿破乌云
纵观我国的O2O行业发展历程,去年上半年还处于资本的投资热潮,下半年就遭遇到了寒冬的突袭,使得很多才刚刚发芽的O2O企业直接被一阵寒风给吹倒.但同样的,一阵风浪过后才知道在O2O这片战场上谁才是有实力 ...
- jQuery/javascript实现全选全不选
<html> <head> <meta charset="utf-8"> <title>Checkbox的练习</title& ...
- JAVA学习Swing绝对局部简单学习
package com.swing; import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; ...
- Python面试题(二)
打印九九乘法表 思路:利用字符串的连接,梯形输出结果 >>> def st(num): ... l = [] ... for x in xrange(1, num + 1): ... ...