您的位置:首页 > 其它

Leetcode 39. Combination Sum

2016-12-31 06:21 316 查看
Summarize of backtracking problems. 

Backtracking. 

public class Solution {
public static void backTrack(List<Integer> tmp, List<List<Integer>> res, int start, int target, int[] nums) {
if (target < 0) return;
else if (target == 0) res.add(new ArrayList<>(tmp));
else {
for (int i=start; i<nums.length; i++) {
tmp.add(nums[i]);
backTrack(tmp, res, i, target-nums[i], nums);
tmp.remove(tmp.size()-1);
}
}
}

public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
backTrack(new ArrayList<>(), res, 0, target, candidates);
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: