Leecode------array, dfs---asks all combinations to be target, with duplicate arrays

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.