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())()"]
")(" -> [""] 首先,遍历所有情况,然后看是否最后得到的string是否满足所有valid parentheses的情况,满足的话,就比较已经存在的valid parentheses. 否则就放弃。
 public class Solution {
public List<String> removeInvalidParentheses(String s) {
List<String> res = new ArrayList<String>();
int[] max = { };
dfs(s, "", , res, max);
if (res.size() == ) {
res.add("");
}
return res;
} private void dfs(String str, String subRes, int countLeft, List<String> res, int[] max) {
if (countLeft > str.length()) return;
if (str.length() == ) {
if (countLeft == && subRes.length() != ) {
if (subRes.length() > max[]) {
max[] = subRes.length();
res.clear();
}
if (max[] == subRes.length() && !res.contains(subRes)) {
res.add(subRes.toString());
}
}
return;
} if (str.charAt() == '(') {
dfs(str.substring(), subRes + "(", countLeft + , res, max);
dfs(str.substring(), subRes, countLeft, res, max);
} else if (str.charAt() == ')') {
if (countLeft > ) {
dfs(str.substring(), subRes + ")", countLeft - , res, max);
}
dfs(str.substring(), subRes, countLeft, res, max);
} else {
dfs(str.substring(), subRes + str.charAt(), countLeft, res, max);
}
}

重写了一遍,思路有点不一样。

 public class Solution {
public List<String> removeInvalidParentheses(String s) {
List<String> list = new ArrayList<>();
if (s == null) return list; list.add("");
helper(s, , "", , list);
return list;
} void helper(String str, int index, String tempString, int leftCount, List<String> list) {
if (str.length() < || index > str.length()) return;
// if the number of "(" is greater than the remaining number of letters in the string, we should return
// ex: "((((", but in the str, the remaining letters are less than leftCount
if (leftCount > str.length() - index) return; // if the length(remaining letters + tempString) < length(exist strings) in the list
// ex: str = "(((()))" index = 6 tempString = "(" list : "(())"
if (list.size() != && list.get().length() > tempString.length() + str.length() - index) return; // reached the end of the original string
if (index == str.length() && leftCount == ) {
if (list.get().length() < tempString.length()) {
list.clear();
list.add(tempString);
} else if (list.get().length() == tempString.length() && !list.contains(tempString)) {
list.add(tempString);
}
return;
} // add the current letter to the tempString or not
if (str.charAt(index) == '(') {
helper(str, index + , tempString + "(", leftCount + , list); // add
helper(str, index + , tempString, leftCount, list); // does not add
} else if (str.charAt(index) == ')') {
if (leftCount != ) {
helper(str, index + , tempString + ")", leftCount - , list); // add
}
helper(str, index + , tempString, leftCount, list); // does not add
} else { // has special letters
helper(str, index + , tempString + str.charAt(index), leftCount, list); // add
}
}

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. 301. Remove Invalid Parentheses

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

  4. Remove Invalid Parentheses 解答

    Question Remove the minimum number of invalid parentheses in order to make the input string valid. R ...

  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. 使用 Elmah一些要注意的问题

    http://www.cnblogs.com/apsnet/archive/2012/04/28/2474730.html 1. Elmah使用后,在发布时,要区分IIS6和IIS7 ,IIS6下 H ...

  2. 前端排版-使用inline-block且两端对齐

    那天排遇到这样一个页面,每个logo紧挨着,而且两端对齐.尼玛,没招啊~ 今天终于找到了解决办法: <!DOCTYPE html> <html> <head> &l ...

  3. Memcached目录

    Memcached 简介.安装和基本使用 Memcached基础知识 理解Memcached的分布式 Memcached存储命令 - set Memcached存储命令 - add Memcached ...

  4. git 初始化

    Git global setup git config --global user.name "杨清1" git config --global user.email " ...

  5. jquery获取datagrid多选值

    var checkedItems = $('#dg').datagrid('getChecked'); $.each(checkedItems, function (index, item) { al ...

  6. dwz 在dialog里打开dialog

    需要在打开dialog里再弹出一个dialog的话,需要在打开第一个dialog的地方指定rel,这样就可以弹出第二个dialog而不是替换掉第一个dialog <a class="a ...

  7. 【codevs1163】访问艺术馆

    题目描述 皮尔是一个出了名的盗画者,他经过数月的精心准备,打算到艺术馆盗画.艺术馆的结构,每条走廊要么分叉为二条走廊,要么通向一个展览室.皮尔知道每个展室里藏画的数量,并且他精确地测量了通过每条走廊的 ...

  8. 3.聚类–K-means的Java实现

    K-means的步骤 输入: 含n 个样本的数据集,簇的数据K 输出: K 个簇 算法步骤: 1.初始化K个簇类中心C1,C2,---Ck (通常随机选择) 2.repeat 步骤3,4 3,将数据集 ...

  9. 请求WebMethod, Ajax 处理更加专注

    在WebForm下 开发ajax程序,需要借助于一般处理程序(*.ashx)或web服务(*.asmx),并且每一个ajax请求,都要建一个这样的文件,如此一来,如 果在一个项目中ajax程序多了,势 ...

  10. Linux 动态监听进程shell

    背景 前几天在研究线程的时候,看到一句话说java里的线程Thread.run都会在Linux中fork一个的轻量级进程,于是就想验证一下(笔者的机器是Linux的).当时用top命令的时候,进程总是 ...