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