class Solution { public List<List<Integer>> combinationSum2(int[] nums, int target) { //cc int result = 0; int difference = Math.MAX_VALUE; if (nums == null || nums.length == 0) return result; //排序一下 Arrays.sort(nums); backtrace(nums, new ArrayList<Integer>(), 0, 0, target, difference, result); return result; } public void backtrace(int[] nums, List<Integer> temp, int start, int currentSum, int target, int difference, int result) { //exit if (currentSum > target) { return ; }else { if (target - currentSum < difference) { difference = target - currentSum; result = currentSum; } for (int i = start; i < nums.length; i++) { if (i > start && nums[i] == nums[i - 1]) //重复的压根不能用 continue; temp.add(nums[i]); backtrace(nums, temp, i, currentSum + nums[i], target, difference, result); temp.remove(temp.size() - 1); } } } }