Leetcode 301. Remove Invalid Parentheses
来源:互联网 发布:大数据对日常生活影响 编辑:程序博客网 时间:2024/04/30 12:32
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())()"]")(" -> [""]
Credits:
Analysis: this question is quite similar to all the Parentheses question. we need to make sure that the left ( is more than ), then if the number of the left ( is equal to the number of the right one ), there is a valid parentheses. then we just compare is to the maxValue.
1 This problem has a trick that how to delete the duplicates. this is quite common in backtracking
if(i != start && s.charAt(i)== s.charAt(i - 1))
continue;
2 also consider the "", when the input string is "", we need to return a "" in the result
code
public class Solution { int maxNum = 0; public List<String> removeInvalidParentheses(String s) { int left = 0; int right = 0; List<String> resultList = new ArrayList<String>(); resultList.add(""); int length = s.length(); if(length == 0){ return resultList; } int start = 0; //start is to record the begining bit to visit the String dfs( s, "", start, left, right,resultList,0); return resultList; } public void dfs(String s, String result, int start, int left, int right,List<String> resultList, int number){ if(left == right && number != 0){ if(number > maxNum){ maxNum = number; resultList.clear(); resultList.add(result); } else if(number == maxNum){ resultList.add(result); } } int length = s.length(); if(start >= length) return; for(int i = start; i < length; i++){ if(i != start && s.charAt(i)== s.charAt(i - 1)) continue; if(s.charAt(i) == '(') dfs(s,result + "(", i + 1, left + 1, right,resultList,number + 1); else if(s.charAt(i) == ')' && left > right) dfs(s,result + ")", i + 1, left, right + 1,resultList,number + 1); else if(s.charAt(i) != '(' && s.charAt(i) != ')') dfs(s,result + s.charAt(i),i + 1,left,right,resultList, number + 1); } }}
- Leetcode 301. Remove Invalid Parentheses
- leetcode 301. Remove Invalid Parentheses
- [leetcode] 301. Remove Invalid Parentheses
- leetcode.301. Remove Invalid Parentheses
- [LeetCode]--301. Remove Invalid Parentheses
- Leetcode 301. Remove Invalid Parentheses
- 【LeetCode】301. Remove Invalid Parentheses
- [LeetCode]301. Remove Invalid Parentheses
- Leetcode 301. Remove Invalid Parentheses
- leetcode 301. Remove Invalid Parentheses
- [leetcode]301. Remove Invalid Parentheses
- Leetcode: 301.Remove Invalid Parentheses
- [LeetCode] Remove Invalid Parentheses
- LeetCode -- Remove Invalid Parentheses
- leetcode Remove Invalid Parentheses
- LeetCode Remove Invalid Parentheses
- [Leetcode]Remove Invalid Parentheses
- leetcode:Remove Invalid Parentheses
- 利用qt进入cmd(dos命令行控制台)自动输入cmd指令
- Java 单例模式详解
- qt 上下页布局与代码规范
- 促销体系之单次购物券
- android---listView分割线的显示和隐藏
- Leetcode 301. Remove Invalid Parentheses
- 226,GCD实例演示
- 什么是尾递归(python引例)
- jQuery Ajax 实例 全解析
- RH413企业安全加固 第10章 第二节 管理 PAM
- 促销体系之商品预售
- 网络加密解密原理(二) RSA加密解密及数字签名Java实现
- 1104. Sum of Number Segments (20)
- 解决"应用程序无法启动,因为应用程序的并行配置不正确"问题