本文主要是介绍leetcode39 组合数总和,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
leetcode39 组合数总和
题目描述
public List<List<Integer>> combinationSum(int[] candidates, int target){List<List<Integer>> res = new ArrayList<>();int len = candidates.length;Arrays.sort(candidates);backtrace(candidates, len, 0, target, new ArrayDeque<Integer>(), res);return res;}/***** @param candidates* @param len* @param begin* @param residue* @param res*/private void backtrace(int[] candidates, int len, int begin, int residue, Deque<Integer> path, List<List<Integer>> res){if(residue == 0){res.add(new ArrayList<Integer>(path));return;}for(int i = begin; i < len; i ++){if(residue - candidates[i] < 0)break;path.addLast(candidates[i]);backtrace(candidates, len, i, residue - candidates[i], path, res);path.removeLast();}}
这篇关于leetcode39 组合数总和的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!