题目:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]
题解:
Solution 1 (TLE)
class Solution {public: void dfs(vector>& vv, vector & v, vector candidates, int target, int sum, vector & visited) { if(sum == target) { vector * tmp = new vector ; *tmp = v; sort((*tmp).begin(), (*tmp).end()); if(find(vv.begin(), vv.end(), *tmp) == vv.end()) vv.push_back(*tmp); delete tmp; return; } if(sum > target) return; for(int i=0; i