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 ...
随机推荐
- Struts2 中Parameters是如何获取值的
刚刚学习struts2的知识,在练习struts2的默认语言OGNL过程中,对于<p>parameters:<s:property value="#parameters.u ...
- Lua之元表
Lua之metatable 一.元表 Lua中的每个值都有一套预定义的操作集合,也可以通过metatable(元表)来定义一个值的行为,metatable包含了一组meatmethod(元方法). L ...
- redis入侵小结
redis安装: windows安装包:http://pan.baidu.com/s/1i3jLlC5 下载下来之后,开始安装: redis-server.exe redis.conf: 简单一步,安 ...
- Guid.NewGuid()
System.Guid.NewGuid().ToString()全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装.在许多流行软件应用程序(例如 Web 浏览器和媒体播放器) ...
- Mongodb副本集搭建经验
一.环境配置经验 1.一般安装的副本集的时候,主实例可以有数据库和用户:从实例不能.仲裁机不能有任何数据库包括用户 2.搭建副本集的时候Host使用外网IP,否则使用Mongodb VUE 1.6.9 ...
- PHP如何实现页面静态化
1.file_put_contents()函数 2.fwrite()函数 3.使用PHP内置缓存机制实现页面静态化-output_buffering
- C语言动态内存分配
考虑下面三段代码: 片段1 void GetMemory(char *p) { p = (); } void Test(void) { char *str = NULL; GetMemory(str) ...
- jquery eval解析JSON中的注意点介绍
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式:使用eval()函数.使用Function对象来进行返回解析,下面有个示例,感兴趣的朋友可以参考下 在JS中将JSON的字符串解析 ...
- 【转载】Ubuntu 系列安装 Docker
系统要求 Docker 支持以下版本的Ubuntu操作系统: Ubuntu Xenial 16.04 (LTS) Ubuntu Wily 15.10 Ubuntu Trusty 14.04 (LTS) ...
- HTTP状态301、404、200、304分别表示什么意思
301 (永久移动)请求的网页已永久移动到新位置.服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置.您应使用此代码告诉 Googlebot 某个网页或网站已永久移动 ...