Remove Invalid Parentheses
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的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)
Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses
题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...
- Remove Invalid Parentheses 解答
Question Remove the minimum number of invalid parentheses in order to make the input string valid. R ...
- [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [leetcode]301. Remove Invalid Parentheses 去除无效括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- LeetCode301. Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses去除不符合匹配规则的括号
[抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...
- [LeetCode] 301. Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
随机推荐
- C# 向IQueryable添加一个Include扩展方法
using System; using System.Data.Objects; using System.Linq; namespace OutOfMemory.Codes { /// <su ...
- 第一篇随笔!!!THE FIRST BLOOD!!!
注册成功,哈哈 以后就要在这扎窝了,要坚持记录下去,关于生活,关于学习. 选了个皮肤 darkgreen,不错,简洁舒服,喜欢. 好,差不多了,感觉没什么可说的了...==
- js实现-下拉列表左右选择
下拉列表左右选择 * 下拉选择框 <select> <option>111</opt ...
- css display 总结
1. 块级元素(display: block) 1.1. 独占一行 1.2. 高度.宽度.行高.顶和底边距 可设置 1.3. 默认宽度 父容器100% 2. 内联元素(display: inline) ...
- Python开发【第八篇】:网络编程 Socket
Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...
- Java读取txt文件,计算2011年9月份的通话时间
public class test2 { /** * @param args * @throws IOException */ public static void main(String[] arg ...
- TCSQL实时列表缓存数据库帮助文档
[文章作者:张宴 本文版本:v1.1 最后修改:2010.09.03 转载请注明原文链接:http://blog.zyan.cc/tcsql/] 曾经有人提出,一般数据库缓存分为四种.第一种:单个对象 ...
- mac jdk环境变量
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk ...
- Javascript面向对象编程一:基础篇
该随笔分为以下四部分: Javascript面向对象编程一:基础篇 Javascript面向对象编程二:封装 Javascript面向对象编程三:继承 Javascript面向对象编程四:控件 先弄个 ...
- Java小程序--抓取emai
一.实现思路 1.使用Java.net.URL对象,绑定网络上某一个网页的地址 2.通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象 3. ...