题目:

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的更多相关文章

  1. [LeetCode] 301. Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  2. [leetcode]301. Remove Invalid Parentheses 去除无效括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  3. 301. Remove Invalid Parentheses去除不符合匹配规则的括号

    [抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...

  4. LeetCode 301. Remove Invalid Parentheses

    原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...

  5. 301 Remove Invalid Parentheses 删除无效的括号

    删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...

  6. 【leetcode】301. Remove Invalid Parentheses

    题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...

  7. Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)

    Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...

  8. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  9. Remove Invalid Parentheses

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

随机推荐

  1. c 计算 语句 执行 时间

    当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:   #include “stdio.h” #include “stdlib.h” #include “tim ...

  2. 11.9Daily Scrum

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.823 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.824 实现视频浏览的功能 王 ...

  3. 11.2Daily Scrum

    人员 任务分配完成情况 明天任务分配 王皓南 做文件的网页和数据库连接,任务编号771 实现视频上传的功能 申开亮 实现视频浏览的功能,任务编号772 实现视频浏览的功能 王宇杰 后台测试,任务编号7 ...

  4. HTML统一资源定位器

    w3c 更好的解释 在OSGi in action中安装bundle时要加文本传输协议,要不然shell判断不出来

  5. 异步FIFO为什么用格雷码

    异步FIFO通过比较读写地址进行满空判断,但是读写地址属于不同的时钟域,所以在比较之前需要先将读写地址进行同步处理,将写地址同步到读时钟域再和读地址比较进行FIFO空状态判断(同步后的写地址一定是小于 ...

  6. RobotFramework-登录

    *** Settings *** Library Selenium2Library *** Test Cases *** login [Setup] open browser http://XXX/X ...

  7. 待验证的一些IOS问题

    1.images.assert中的图片格式必须是png.(jpg格式的图片不行)

  8. Java Applet使用

    问题描述:       Java Applet使用   参考资料:      http://docs.oracle.com/javase/tutorial/deployment/applet/depl ...

  9. Leetcode#91 Decode Ways

    原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...

  10. vs2008 添加与修改模板.

    添加 我的模板: 路径:  C:\Users\Administrator\Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C# ...