[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 ();
}
}
};