The entry of Dfs is this: total result, current result, current sum, array, array subscript, target if the current result >target directly exits if ==target, the sum of the records results is less than the target description currently needs to be added in the number, However, the number of digits that can be added can be added to the array from the POS position. This way, because there may be duplicates, if the current number and the previous number repeats, the direct continue skips. https://leetcode.com/problems/combination-sum-ii/description/ test instructions gives an array and a target number, Where the array has duplicates, find all the results of the combination for target analytic backtracking method. The array is sorted first, and if repeated, it crosses the past. Because it is not possible to reuse numbers, it is time to go backwards. Code class Solution { list<list<integer>> result = new linkedlist<list<integer>> (); int Target; public list<list<integer>> combinationSum2 (int[] candidates, int target) { if (candidates = = NULL | | candidates.length <= 0 | | Target < 0) return ResU Lt; this.target = target; arrays.sort (candidates); &nbsP; backtrack (Candidates,0,new linkedlist<integer> (), 0); return result; } //backtracking, backtracking process: &NBSP;&NBSP;&NBSP;&NBSP;//1. Direct return without satisfying the condition, this side cursum exceeds TARGET&NBSP;&NBSP;&NBSP;&NBSP;//2. The direct record of the meeting to the global variable //3. Otherwise continue backtracking, backtrack record path when remember to join and eject public void BackTrack (int[] Nums,int cursum, List<integer> temp,int start) { if (CurSum > Target) return;& Nbsp; if (cursum = = target) { result.add (new LinkedList (temp)); return; } for (int i = start; i < nums.length; i++) { if (i > Start && nums[i] = = Nums[i-1]) continue; temp.add (Nums[i]); backtrack (nums,cursum + nums[i],temp,i + 1); temp.remove (Temp.size ()-1); } }}
Leecode------array, DFS---all combinations as target, with duplicate arrays