-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
50 lines (47 loc) · 1.2 KB
/
Copy pathmain.cpp
File metadata and controls
50 lines (47 loc) · 1.2 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
//
// main.cpp
// leetcode040
//
// Created by 萧天牧 on 17/3/2.
// Copyright © 2017年 萧天牧. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
void Helper(vector<vector<int> > &ret, vector<int> cur, vector<int> &num, int target, int position)
{
if(target == 0)
ret.push_back(cur);
else
{
for(int i = position; i < num.size() && num[i] <= target; i ++)
{
if(i != position && num[i] == num[i-1])
continue;
cur.push_back(num[i]);
Helper(ret, cur, num, target-num[i], i+1);
cur.pop_back();
}
}
}
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
sort(num.begin(), num.end());
vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, num, target, 0);
return ret;
}
void printRes(vector<vector<int>> res){
for (int i = 0; i < res.size(); i++){
for (int j = 0; j < res[i].size(); j++){
cout <<res[i][j] <<" ";
}
cout << endl;
}
}
int main(int argc, const char * argv[]) {
vector<int > a = {10,1,2,7,6,1,5};
combinationSum2(a, 8);
std::cout << "Hello, World!\n";
return 0;
}