题目:

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. web 性能忧化(IIS篇)

    1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings 2.   ...

  2. Mac系统如何配置adb

    在使用mac进行android开发之前,我们一般会安装android studio 或者 eclipse,无论哪一款开发软件,都少不了安装adb(Android Debug Bridge).adb(A ...

  3. 在Action中以Struts2的方式输出JSON数据

    参考地址;http://blog.csdn.net/itdada/article/details/21344985

  4. IBM MQ消息中间件jms消息中RHF2消息头的处理

    公司的技术平台在和某券商对接IBM MQ消息中间件时,发送到MQ中的消息多出了消息头信息:RHF2,造成消息的接收处理不正常.在此记录此问题的处理方式. 在IBM MQ中提供了一个参数 targetC ...

  5. 我是一只it小小鸟阅读笔记

    “我们具有各自的独特性--我们兴趣各异,有不同的家庭背景,不同的知识储备,不同的思维方式……但在现实中,我们也会碰到类似的人生选择的关口,我们会犯类似的错误,有类似的迷惘,也会为类似的精彩鼓掌,而且很 ...

  6. 【CentOS】Eclipse中svn插件使用

    目录: 1.安装 2.使用 3.错误 1.安装 svn插件地址: Subclipse 1.6.x Update Site - http://subclipse.tigris.org/update_1. ...

  7. 【Tsinsen】【A1365】森林旅店

    KD-Tree 啊哈~检验了一下自己KD-Tree的学习情况,还算可以,模板至少是记下来了. 支持插入(所以要带重建),查询最近的P个点的距离. 然而题目并没有说是按怎样的顺序输出这P个点?...(事 ...

  8. RVA与Offset的换算函数

    function RVAToFileOffset(FileName:string; RVA: Cardinal): Cardinal; var   MemPE: TFileStream;   PEDo ...

  9. ios 环境配置网址

    http://blog.csdn.net/cwb1128/article/details/18019751

  10. HDOJ 1085 Holding Bin-Laden Captive! (母函数)

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...