import java.util.*; public class Solution { private ArrayList<ArrayList<Integer>> res = new ArrayList<>(); /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param target int整型 * @param nums int整型一维数组 * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> combinationCount (int target, int[] nums) { // write code here if (nums == null || nums.length < 1) { return res; } Arrays.sort(nums); backtrack(nums, 0, target, new ArrayList<>()); return res; } private void backtrack(int[] nums, int start, int target, List<Integer> list) { if (list.size() > 0 && target == 0) { res.add(new ArrayList<>(list)); return; } for (int i = start; i < nums.length; i++) { if (target - nums[i] < 0) { break; } target -= nums[i]; list.add(nums[i]); backtrack(nums, i, target, list); target += nums[i]; list.remove(list.size() - 1); } } }