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 ...
随机推荐
- Net Core 的公共组件之 Http 请求客户端
Net Core 的公共组件之 Http 请求客户端 想必大家在项目开发的时候应该都在程序中调用过自己内部的接口或者使用过第三方提供的接口,咱今天不讨论 REST ,最常用的请求应该就是 GET 和 ...
- iptables端口转发
iptables -t nat -A PREROUTING -d {外网ip}/32 -p tcp -m tcp --dport 4956 -j DNAT --to-destination 10.1. ...
- 如何维护SSH安全
遇到两次,一次是公司服务器搭建好后,有人尝试ssh暴力破解,auth.log不停出现错误提示 还有买的米国vps,很荣幸地遭到来自波兰的ssh破解尝试 不得不重视ssh的安全 方法: 修改sshd_c ...
- android实现左右滑动菜单
直接看效果图: 主要实现代码: package com.way.view; import android.content.Context; import android.media.Den ...
- Phpstorm开发记
Phpsotrm虽然付费项目,但网上有免费的激活码,也可以免费用不是. 1.首先是svn,windows项目下用Phpsotrm需要安装svn时,支付svn命令的,否则会提示找不到svn命令.2.建项 ...
- UVALive - 6955 Finding Lines 随机算法
题目链接: http://acm.hust.edu.cn/vjudge/contest/126968#problem/F 题意 给你n个点,问是否有>=p/100*n个点共线(p>=20& ...
- URAL 1920 Titan Ruins: the Infinite Power of Magic
大意: 有一张N*N的网格,你每次可以走一步,每格只能走一次,有没有一种方法让走了L步后回到一个距原点1步远的格子? 没有输出Unsuitable device,否则输出Overwhelming p ...
- boost环境搭建
切换到boost目录下面,使用编译命令>bjam.exe --with-date_time --toolset=msvc-9.0 --build-type=complete stage --wi ...
- 响应式设计Responsinator工具推荐
from:http://www.25xt.com/allcode/4066.html 原文推荐了5种,感觉有用的吧就这一种,所以收藏过来. Responsinator工具的好处Responsinato ...
- 2014 ACM/ICPC Asia Regional Guangzhou Online
Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...