301. Remove Invalid Parentheses
题目:
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses (
and )
.
Examples:
"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]
链接: http://leetcode.com/problems/remove-invalid-parentheses/
题解:
给定String,去除invalid括号,输出所有结果集。一开始想的是DFS + Backtracking,没有坚持下去。后来在Discuss里发现了jeantimex大神的BFS方法非常好,于是搬过来借鉴。方法是我们每次去掉一个"("或者")",然后把新的string加入到Queue里,继续进行计算。要注意的是需要设置一个boolean foundResult,假如在这一层找到结果的话,我们就不再继续进行下面的for循环了。这里应该还可以继续剪枝一下,比如记录当前这个结果的长度len,当queue里剩下的string长度比这个len小的话,我们不进行验证isValid这一步。
Time Complexity - O(n * 2n), Space Complexity - O(2n)
public class Solution {
public List<String> removeInvalidParentheses(String s) {
List<String> res = new ArrayList<>();
if(s == null) {
return res;
}
Queue<String> queue = new LinkedList<>();
Set<String> visited = new HashSet<>();
queue.offer(s);
boolean foundResult = false; while(!queue.isEmpty()) {
s = queue.poll();
if(isValid(s)) {
res.add(s);
foundResult = true;
}
if(foundResult) {
continue;
}
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == '(' || c == ')') {
String t = s.substring(0, i) + s.substring(i + 1);
if(!visited.contains(t)) {
queue.offer(t);
visited.add(t);
}
}
}
} return res;
} private boolean isValid(String s) {
int leftCount = 0;
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == '(') {
leftCount++;
} else if (c == ')') {
leftCount--;
}
if(leftCount < 0) {
return false;
}
} return leftCount == 0;
}
}
Reference:
https://leetcode.com/discuss/67842/share-my-java-bfs-solution
https://leetcode.com/discuss/67853/my-c-dfs-solution-16ms
https://leetcode.com/discuss/67919/java-optimized-dfs-solution-3-ms
https://leetcode.com/discuss/67861/short-python-bfs
https://leetcode.com/discuss/72208/easiest-9ms-java-solution
https://leetcode.com/discuss/67861/short-python-bfs
https://leetcode.com/discuss/72208/easiest-9ms-java-solution
https://leetcode.com/discuss/67908/java-bfs-solution-16ms-avoid-generating-duplicate-strings
https://leetcode.com/discuss/67821/and-bfs-java-solutions-add-more-optimized-fast-dfs-solution
https://leetcode.com/discuss/68038/clean-java-solution-bfs-optimization-40ms
https://leetcode.com/discuss/68010/fast-optimized-dfs-java-solution
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 去除无效括号
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. Retu ...
- LeetCode 301. Remove Invalid Parentheses
原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...
- 301 Remove Invalid Parentheses 删除无效的括号
删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...
- 【leetcode】301. Remove Invalid Parentheses
题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...
- Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)
Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...
- [LeetCode] 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 ...
随机推荐
- Ant学习---第二节:Ant添加文件夹和文件夹集的使用
一.创建 java 项目(Eclipse 中),结构图如下: 1.创建 .java 文件,代码如下: package com.learn.ant; public class HelloWorld { ...
- “来用”alpha版使用说明书
1引言 1 .1编写目的 针对我们发布的alpha版本做出安装和使用说明,使参与内测的人员及用户了解软件的使用方法和相关内容. 1 .2参考资料 <c#程序设计基础>,赵敏主编,2011, ...
- Careercup - Microsoft面试题 - 5428361417457664
2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...
- JavaScript原型与原型链学习笔记
一.什么是原型?原型是一个对象,其他对象可以通过它实现属性继承.简单的说就是任何一个对象都可以成为原型 prototype属性: 我们创建的每个函数都有一个prototype属性,这个属性是一个指针, ...
- 使用 NIO.2 遍历目录下所有的Java文件
package wellGrounded; import java.io.IOException; import java.nio.file.FileVisitResult; import java. ...
- Upgrading to Java 8——第一章 Lambda表达式
第一章 Lambda表达式 Lamada 表达式是Java SE 8中最重要的新特性,长期以来被认为是在Java中缺失的特性,它的出现使整个java 语言变得完整.至少到目前,在这节中你将学习到什么是 ...
- [转载]Extjs中的dom,Ext.Element和Ext.Component对象的关系
原文地址:http://www.cnblogs.com/lwzz/archive/2011/01/30/1948106.html Ext.Element对象是对dom对象的封装,目的是为了跨平台以 ...
- MyEclipse2015 编写js报 'Calculating completion proposals..' has encountered a problem.
前言:编写js(按点后)弹出这个鬼东西,百度不到..估计是破解有问题.只有换版本了. 版本:MyEclipse 2015 stable 1.0 详细错误信息 解决:换成2.0版本
- 土地购买 usaco 斜率优化
看这道题的时候,感觉很难,因为数据范围比较大,很难dp: 后来想到了[书柜的尺寸]这道题,也是一道dp,曾经看了那道题的题解而深有启发: 这道题每组的付费只与这一组长宽的最大值有关,也就是说要分组,一 ...
- Request/Server的相关topic
Request---------Server模式 HTTP 协议--------->这个可能返回json, 也可能是HTML HTML页面处理的流程以及资源文件的加载 浏览器最大连接数 js资源 ...