[Leetcode] Combination Sum II

Source: Internet
Author: User

[Leetcode] Combination Sum II

// Questions...

// Same as combination sum, dfs

// But it cannot be reused at this time. However, if two values exist, the same value can be used again in the example, but it cannot be used multiple times.

// In the Book, pre is used for determination, but won't it miss the situations of and 6 in the example ?...

// Functions with or without dfs run according to the number of occurrences of each subject... think about it...

Class Solution {

Public:
Vector > CombinationSum2 (vector & Num, int target ){
Int len = num. size ();
Vector > Result;
Vector Temp;
Sort (num. begin (), num. end ());
Dfs (num, target, 0, temp, result );
Return result;
}
Void dfs (vector & Num, int gap, int start, vector & Temp, vector > & Result)
{
If (gap = 0)
{
// If (result. find (temp) = result. end () // In fact, this is the meaning of so many, but obviously the vector > No corresponding find function exists. An error is reported during compilation.
Result. push_back (temp );
Return;
}
Int pre =-1;
For (int I = start; I If (gap If (pre = num [I]) continue;
Pre = num [I];
Temp. push_back (num [I]);
Dfs (num, gap-num [I], I + 1, temp, result );
Temp. pop_back ();
}
}
};

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.