LeetCode刷题记65
40. 组合总和 II
class Solution {
public void F(List<List<Integer>> ans, int[] candidates, int target, int i, int cur, List<Integer> nums) {
for (; i < candidates.length; i ++) {
if (cur + candidates[i] == target) {
nums.add(candidates[i]);
ans.add(nums);
return;
}
if (cur + candidates[i] > target) {
return;
} else {
List<Integer> tmp = new ArrayList(nums);
tmp.add(candidates[i]);
F(ans, candidates, target, i + 1, cur + candidates[i], tmp);
while (i < candidates.length - 1 && candidates[i] == candidates[i + 1]) {
i ++;
}
}
}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> ans = new ArrayList();
F(ans, candidates, target, 0, 0, new ArrayList<Integer>());
return ans;
}
}
今天怕是做不了五个题目了。。。
2/5
65/150