一、题目内容
二、题解
解法1:
思路
代码
import java.util.*;public class Solution { private int[] nums; private ArrayList<Integer> tempAns; private ArrayList<ArrayList<Integer>> ans; private int target; public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) { this.tempAns = new ArrayList<Integer>(); this.ans = new ArrayList<ArrayList<Integer>>(); this.nums = num; this.target = target; Arrays.sort(nums); recur(0,0); return ans; } public void recur(int index, int sum){ if(sum > target){ return; } if(sum == target){ ans.add(new ArrayList(tempAns)); return; } for(int i = index; i<nums.length; i++){ //移除重复 if(i>index && nums[i] == nums[i-1]){ continue; } tempAns.add(nums[i]); recur(i+1,sum+nums[i]); tempAns.remove(tempAns.size()-1); } }}