39 Combination Sum
2015-09-04 15:45
281 查看
public class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); public List<List<Integer>> combinationSum(int[] candidates, int target) { if(candidates==null||candidates.length==0) return res; Arrays.sort(candidates); int len = candidates.length; List<Integer> list = new ArrayList<Integer>(); dfs(list, candidates, 0, target); return res; } public void dfs(List<Integer> list, int[] candidates, int sum, int target){ int len = candidates.length; for(int i=0;i<len;++i){ if(sum + candidates[i]==target){ List<Integer> tmp = new ArrayList<Integer>(list); tmp.add(candidates[i]); Collections.sort(tmp); if(!res.contains(tmp)){ res.add(tmp); } }else if(sum + candidates[i] < target){ List<Integer> t1 = new ArrayList<Integer>(list); t1.add(candidates[i]); dfs(t1, candidates, sum + candidates[i], target); }else{ break; } } } }
相关文章推荐
- 最小生成树(普利姆算法、克鲁斯卡尔算法)
- 关于锁屏和回到桌面的activity生命周期的疑问
- **Git 使用规范流程
- Objective-C常用类的常用方法
- JS实现先显示大图后自动收起显示小图的广告代码
- ZigZag字符长转换
- Linux用户和用户组管理
- C++基础---构造函数与析构函数
- 【数据结构】图
- 成功的背后!(给所有IT人)
- POJ 题目3368 ||HDOJ 题目1806 Frequent values(RMQ)
- 机房收费总结
- Java线程通信
- JQUERY的ajax方法的配置参数
- React 实践记录 04 Flux demo
- mvcAPI (入门 1)
- 非注解SpringMVC
- Linux下MySQL数据库常用基本操作
- python多线程操作【概念】
- HDU1036 - Average is not Fast Enough! (简单题)