40. Combination Sum II
2016-02-28 08:47
323 查看
和39. Combination Sum基本是一样的,就是上一次迭代的时候从这个数的位置开始,不可以重复的话,就从下一个位置开始
基本无bug一遍通过的
public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if(candidates == null || candidates.length == 0) { return res; } Arrays.sort(candidates); helper(candidates, target, res, new ArrayList<Integer>(), 0, 0); return res; } public void helper(int[] candidates, int target, List<List<Integer>> res, List<Integer> item, int start, int curSum) { if(curSum > target) { return; } if(curSum == target) { if(!res.contains(item)) { res.add(new ArrayList<Integer>(item)); } return; } for(int i = start; i < candidates.length; i++) { item.add(candidates[i]); helper(candidates, target, res, item, i + 1, curSum + candidates[i]); item.remove(item.size() - 1); } return; }
基本无bug一遍通过的
相关文章推荐
- 17.蛇形矩阵(模拟)
- 17.蛇形矩阵(模拟)
- 17.蛇形矩阵(模拟)
- 17.蛇形矩阵(模拟)
- 天声人語 20160228 小舟で気づく温暖化
- Andndroid:ListView缓存机制及BaseAdapter
- POJ 1636 Prison rearrangement
- stm32之assert_param 函数
- Floyd-Warshall算法的原型
- 设计模式总结
- Android UI 使用HTML布局(直接打开server网页)
- Physiological Processes of Speech Production--Reading Notes (8)
- SGU 104 Little shop of flowers
- HDU 2577 How to Type
- ZOJ 3844 Easy Task
- ZOJ 3870 Team Formation
- cookie的学习笔记三(做俩个小练习);
- data structure -- tree
- Android之MVC模式
- epool边沿触发和水平触发