Question

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())()"]
")(" -> [""]

Solution

看到parenthese的问题,第一反应是用栈。这题要求minimum number,所以想到用BFS遍历解空间树。

思路为:

层次依次为删除0个元素,1个元素,2个元素。。。

层次遍历所有的可能。如果有一种可能是valid,那么不再遍历下面的层。

 public class Solution {
public List<String> removeInvalidParentheses(String s) {
Set<String> visited = new HashSet<String>();
List<String> result = new ArrayList<String>();
List<String> current = new ArrayList<String>();
List<String> next;
current.add(s);
boolean reached = false;
// BFS
while (!current.isEmpty()) {
next = new ArrayList<String>();
for (String prev : current) {
visited.add(prev);
// If valid
if (isValid(prev)) {
reached = true;
result.add(prev);
} // If not reached, then delete
if (!reached) {
for (int i = 0; i < prev.length(); i++) {
char tmp = prev.charAt(i);
if (tmp != '(' && tmp != ')') {
continue;
}
String newStr = prev.substring(0, i) + prev.substring(i + 1);
if (!visited.contains(newStr)) {
next.add(newStr);
visited.add(newStr);
}
}
}
}
if (reached) {
break;
}
current = next;
}
return result;
} private boolean isValid(String s) {
Deque<Integer> stack = new LinkedList<Integer>();
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur != '(' && cur != ')') {
continue;
}
if (cur == '(') {
stack.push(i);
}
if (cur == ')') {
if (stack.isEmpty()) {
return false;
} else {
stack.pop();
}
}
}
return stack.isEmpty();
}
}

事实上,我们可以不用栈,用一个count来检测是否是valid parenthese.

 private boolean isValid(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur != '(' && cur != ')') {
continue;
}
if (cur == '(') {
count++;
}
if (cur == ')') {
count--;
if (count < 0) {
return false;
}
}
}
return count == 0;
}

Remove Invalid Parentheses 解答的更多相关文章

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

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

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

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

  3. Remove Invalid Parentheses

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

  4. 301. Remove Invalid Parentheses

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

  5. [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses

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

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

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

  7. LeetCode301. Remove Invalid Parentheses

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

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

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

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

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

随机推荐

  1. hdu5072(鞍山regional problem C):容斥,同色三角形模型

    现场过的第四多的题..当时没什么想法,回来学了下容斥,又听学长讲了一讲,终于把它过了 题目大意:给定n个数,求全部互质或者全部不互质的三元组的个数 先说一下同色三角形模型 n个点 每两个点连一条边(可 ...

  2. Linux内核设计基础(十)之内核开发与总结

    (1)Linux层次结构: (2)Linux内核组成: 主要由进程调度(SCHED).内存管理(MM).虚拟文件系统(VFS).网络接口(NET)和进程间通信(IPC)等5个子系统组成. (3)与Un ...

  3. iOS 8 Auto Layout界面自动布局系列5-自身内容尺寸约束、修改约束、布局动画

    首先感谢众多网友的支持,最近我实在是事情太多,所以没有写太多.不过看到大家的反馈和评价,我还是要坚持挤出时间给大家分享我的经验.如果你对我写的东西有任何建议.意见或者疑问,请到我的CSDN博客留言: ...

  4. Android 避免APP启动闪黑屏的解决办法(Theme和Style)

    前几天Boss就反应说,机器每次启动程序都会闪一下黑屏,这个客户不接受.没办法,只能想想怎么解决,最后找到了下面的方法.闪黑屏的原因主要是我们启动Activity的时候,需要跑完onCreate和on ...

  5. android studio 报ambiguous method call

    如题,在android studio中调用this.toString时,提示的错误信息是ambiguous method call. both get class () in object and g ...

  6. ERROR 1 (HY000): Can't create/write to file '/tmp/#sql_909_0.MYI' (Errcode: 13)

    mysql> desc tablename; ERROR 1 (HY000): Can't create/write to file '/tmp/#sql_909_0.MYI' (Errcode ...

  7. C#语法糖: 扩展方法(常用)

    今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...

  8. 多余的Using Namespaces或引用会影响程序的执行效率么?(转)

    转自:http://www.cnblogs.com/Interkey/p/UsingNameSpace.html 多余的Using Namespaces或引用会影响程序的执行效率么? 在.NET程序编 ...

  9. Csharp 高级编程 C7.1.2(2)

    C#2.0  使用委托推断扩展委托的语法下面是示例  一个货币结构 代理的方法可以是实例的方法,也可以是静态方法,声明方法不同 实例方法可以使用委托推断,静态方法不可以用 示例代码: /* * C#2 ...

  10. SQL中常用的时间格式

    一些常用的时间格式 先讲一下一些基本的格式模式 格式模式      说明 d                   月中的某一天.一位数的日期没有前导零. dd                 月中的某 ...