-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path40.cpp
More file actions
98 lines (86 loc) · 2.44 KB
/
40.cpp
File metadata and controls
98 lines (86 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Author : Accagain
// Date : 17/3/26
// Email : chenmaosen0@gmail.com
/***************************************************************************************
*
* 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,
*
* A solution set is:
* [
* [1, 7],
* [1, 2, 5],
* [2, 6],
* [1, 1, 6]]
*
* 做法:
*
* 时间复杂度:
*
*
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#define INF 0x3fffffff
using namespace std;
class Solution {
public:
bool vis[120000];
void dfs(vector<vector<int>> & ans, vector<int> candidates, int target, int now_sum, int now, vector<int> hav)
{
// for(int i=0; i<hav.size(); i++)
// printf("%d ", hav[i]);
// printf("\n");
if(now == candidates.size())
{
if(now_sum == target)
{
ans.push_back(hav);
for(int i=0; i<hav.size(); i++)
printf("%d ", hav[i]);
printf("\n");
}
return;
}
if(!((now >= 1) && hav.size() > 0 && (candidates[now] == hav[hav.size()-1])))
{
dfs(ans, candidates, target, now_sum, now+1, hav);
}
hav.push_back(candidates[now]);
if(now_sum+candidates[now] <= target)
dfs(ans, candidates, target, now_sum+candidates[now], now+1, hav);
hav.pop_back();
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target)
{
//int vis[candidates.size()];
memset(vis, 0, sizeof(vis));
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> hav;
dfs(ans, candidates, target, 0, 0, hav);
return ans;
}
};
int main() {
Solution *test = new Solution();
int data[] = {10, 1, 2, 7, 6, 1, 5};
vector<int> x(data, data + sizeof(data) / sizeof(data[0]));
test->combinationSum2(x, 8);
return 0;
}
//
// Created by cms on 17/3/26.
//