Remove Invalid Parentheses
2016-07-14 20:19
295 查看
public class Solution { int max = 0; public List<String> removeInvalidParentheses(String s) { List<String> res = new LinkedList<>(); if (s == null) { return res; } 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 maxCount) { if (left.length() == 0) { if (leftCount == 0 && right.length() != 0) { if (maxCount > max) { max = maxCount; } if (maxCount == max && !res.contains(right)) { res.add(right); return; } } return; } if (left.charAt(0) == '(') { helper(res, left.substring(1), right + "(", leftCount + 1, maxCount + 1); helper(res, left.substring(1), right, leftCount, maxCount); } else if (left.charAt(0) == ')') { if (leftCount > 0) { helper(res, left.substring(1), right + ")", leftCount - 1, maxCount); } helper(res, left.substring(1), right, leftCount, maxCount); } else { helper(res, left.substring(1), right + left.charAt(0), leftCount, maxCount); } } }
相关文章推荐
- Qt Mac OS、iOS和X11的Retina显示支持
- Spring学习笔记一: Bean工厂(BeanFactory )与应用上下文(ApplicationContext)BeanFactory
- Spring学习笔记一: Bean工厂(BeanFactory )与应用上下文(ApplicationContext)BeanFactory
- 尚学堂 JAVA DAY12 概念总结
- codeforces 360 D - Remainders Game
- CODE[VS]-保留两位小数-浮点数处理-天梯青铜
- Java并发编程核心方法与框架-Semaphore的使用
- 安装Eclipse并配置JacORB插件
- 怎么来设计一个秒杀系统
- Binary Tree Paths
- Spring Boot实战之入门
- DM 第一课 数据挖掘概括
- HYSBZ 2038 莫队算法
- [置顶] centos使用nginx反向代理实现负载均衡
- LeetCode 24. Swap Nodes in Pairs
- 自定义圆形ImageView(仿QQ头像)
- mybatis中定义mapper 出现bound statement (not found)
- LightOJ1230 Placing Lampposts(DP)
- CODE[VS]-判断浮点数是否相等-浮点数处理-天梯青铜
- Google 推出全新的两步验证机制