[LeetCode]Remove Invalid Parentheses
来源:互联网 发布:网络机顶盒检测 编辑:程序博客网 时间:2024/06/10 08:56
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())()"]")(" -> [""]
public class Solution { public List<String> removeInvalidParentheses(String s) { dfs(0,0,0,s); if(re.isEmpty()) re.add(""); return new ArrayList<String>(re); } HashSet<String> re=new HashSet<String>(); StringBuffer sb=new StringBuffer(); int max=0; public void dfs(int k,int left,int right,String s){ if(k==s.length()){ if(right==left){ if(left*2==max) re.add(sb.toString()); else if(left*2>max){ max=left*2; re.clear(); re.add(sb.toString()); } } return; } char ch=s.charAt(k); sb.append(s.charAt(k)); if(ch=='('){ dfs(k+1,left+1,right,s); }else if(ch==')'){ if(left>right) dfs(k+1,left,right+1,s); }else{ dfs(k+1,left,right,s); } sb.deleteCharAt(sb.length()-1); if(ch=='('||ch==')') dfs(k+1,left,right,s); }}
阅读全文
1 0
- [LeetCode] 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
- 【Leetcode】Remove Invalid Parentheses
- LeetCode:Remove Invalid Parentheses
- Remove Invalid Parentheses -- Leetcode
- [LeetCode]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
- 如何制作PLC信号接口技术文件
- Android Studio 字体大小更改及样式更换
- centos7 安装opencv2.4.13
- 使用 Laravel 数据填充功能生成中文测试数据
- linux shell如何判断文件/文件夹是否存在
- [LeetCode]Remove Invalid Parentheses
- C++中几个关键字的解析
- 文章标题
- 引导滤波Guided Filter
- svn使用详解
- OpenSSL和Python实现RSA Key公钥加密私钥解密
- vue.js中的子组件与父组件,prop传递数据
- 一起来讨论讨论Java多线程技术-20170707
- Linux 下tools路径问题