本文共 698 字,大约阅读时间需要 2 分钟。
找和为target的组合数组
回溯+递归,套路,但是一定注意递归的pos是i不是pos啊!!这种细节都错很不应该啊
public class Solution { public List
> combinationSum(int[] candidates, int target) { List
> res = new LinkedList<>(); combinationSum(candidates, new LinkedList<>(), target, res, 0); return res; } private void combinationSum(int[] candidates, List temp, int target, List
> res, int pos) { if (target == 0) { res.add(new LinkedList<>(temp)); return; } else if (target < 0) { return; } for (int i = pos; i < candidates.length; i++) { temp.add(candidates[i]); combinationSum(candidates, temp, target - candidates[i], res, i); temp.remove(temp.size() - 1); } }}
转载地址:http://yvhld.baihongyu.com/