Remove Invalid Parentheses
2016-07-08 22:11
405 查看
public class Solution { int max = 0; public List<String> removeInvalidParentheses(String s) { if (s == null) { throw new IllegalArgumentException("haha"); } List<String> res = new LinkedList<>(); helper(res, s, "", 0, 0); if (res.size() == 0) { res.add(""); } return res; } private void helper(List<String> res, String left, String right, int leftCount, int leftMax) { if (left.length() == 0) { if (leftCount == 0 && right.length() > 0) { if (leftMax > max) { max = leftMax; } if (leftMax == max && !res.contains(right)) { res.add(right); } } return; } if (left.charAt(0) == '(') { helper(res, left.substring(1), right + '(', leftCount + 1, leftMax + 1); helper(res, left.substring(1), right, leftCount, leftMax); } else if (left.charAt(0) == ')') { if (leftCount > 0) { helper(res, left.substring(1), right + ')', leftCount - 1, leftMax); } helper(res, left.substring(1), right, leftCount, leftMax); } else { helper(res, left.substring(1), right + left.charAt(0), leftCount, leftMax); } } }
相关文章推荐
- HttpClient+Gson解析中国天气网的天气预报信息
- 深入理解计算机系统chapter3
- Quartus II Error总结与解答
- [Leetcode]345. Reverse Vowels of a String
- 归并排序
- 资料库:用CSDN-markdown编辑器写博客
- CUDA入门(二)cuda编程的基本知识与第一个cuda程序
- addrules 创建css
- 222. Count Complete Tree Nodes
- Mongo配置成系统服务
- Mongo配置成系统服务
- LeetCode - Binary Tree Level Order Traversal
- math module
- 禁止root远程登录
- js 内存泄漏
- knockout 学习实例6 attr
- root账户不能使用密码只能使用密钥远程登陆
- [python小工具]加减法出题器
- Matlab删除矩阵的一行或一列以及批量注释
- mac os x常用快捷键及用法