Brush title LeetCode notes - backtracking - the sum of a combination of problems

Subject description:

"Problems sum combination" Given a non-repeating array element candidates and a target number target, can find all the candidates for the target numbers and combinations thereof.

Unlimited numbers of candidates may be selected is repeated.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/combination-sum

Java code implementation

    public static List<List<Integer>> combinationSum(int[] candidates, int target) {
     List
<List<Integer>> result = new ArrayList<>(); Arrays.sort(candidates); //升序排序,便于剪枝 combine2(result,new ArrayList<>(),candidates,target,0); return result; }
public static void combine2(List<List<Integer>> result,List<Integer> temp,int[] candidates,int target,int begin){ if (target==0){ result.add(new ArrayList<>(temp)); return; } for (int i=begin;i<candidates.length && target-candidates[i] >=0 ;i++){ temp.add(candidates[i]); combine2(result,temp,candidates,target-candidates[i],i); temp.remove(temp.size()-1); } }

 

Guess you like

Origin www.cnblogs.com/sqchao/p/11069464.html