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. java实现矩阵连乘的动态规划

    package com.cjs.algorithm; public class DynamicPlan { /** * 此方法用来求解矩阵连乘的最小数乘次数 * * @param p * 传入的要连乘 ...

  2. WebService-使用JDK开发WebService

    一.使用JDK开发WebService 2.1.开发WebService服务器端 1.定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所 ...

  3. Vue + element-ui

    在Vue-cli生成的项目中使用 element-ui,按照官方的指导 npm i element-ui -D 执行之后,查看package.json,element-ui 加在了 "dev ...

  4. 设计: ListView 接口,and the missing read-only interfaces in java collection framework

    Java的集合框架以其成功易用的设计征服了很多人(包括我),并且教科书式的诠释了泛型的应用方式. 我也是被 Joshua Bloch 的书引领入门,从中得益良多.我当然不会认为自己在设计上比他懂得更多 ...

  5. 一个提供jsp免费空间的站点

    EATJ美国JSP虚拟主机商提供免费jsp空间申请,50M空间,每月3G的流量限制,支持Java5.0/6.0.PHP.CGI.Perl.SSI等,提供2个MySQL数据库,Tomcat v5.5/v ...

  6. [Cycle.js] Introducing run() and driver functions

    Currently the code looks like : // Logic (functional) function main() { return { DOM: Rx.Observable. ...

  7. ssh命令

    使用ssh命令登陆远程系统 ssh [ip/address] -l [登陆用户名] 如: ssh www.xyz.cn -l root

  8. delete 用法

    1.对象属性的删除 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);//mm delete ...

  9. 未能加载文件或程序集“System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”

    最近用vs2012发布程序,然后将更新后的程序文件部署到服务器上,由于服务器上本来有此系统,所以只更新了修改的文件 . 进行系统登录时提示:未能加载文件或程序集“System.Web.Extensio ...

  10. win8 安装myeclipse 失败 MyEclipse ForSpring 安装失败

    好像是main方法.jar无法载入之类的.. 可能是权限的问题哦.. 使用管理员权限试一下..