039.Combination Sum I&II&III

这种套餐题,做起来就感觉很爽,简直跟套娃一样,一环套一环。

题目链接

第一题

1
2
3
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Example 1:

1
2
3
4
5
6
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]

特点

Each number in candidates may only be used once in the combination.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
void combination(vector<int>& candidates,vector<vector<int>>& result, vector<int> sol, int target,int pos) {
if (target == 0) { result.push_back(sol); return; }
for (int i = pos; i < candidates.size();i++) {
if (target < candidates[i]) continue;
sol.push_back(candidates[i]);
combination(candidates, result, sol, target - candidates[i],i);
sol.pop_back();
}
}

vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> sol;
combination(candidates, ans, sol, target, 0);
return ans;
}
};

每个数字可以使用无数次,所以使用 pos 记录上次使用的数字,如果需要无数次使用。

第二题

特点

Each number in candidates may only be used once in the combination.

特点

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
class Solution {
public:
void combination(vector<int>& candidates, vector<vector<int>>& result, vector<int> sol, int target, int pos) {
if (target == 0) {
sort(sol.begin(), sol.end());
auto it = find(result.begin(), result.end(), sol);
if(it==result.end())
result.push_back(sol);
return;
}
for (int i = pos; i < candidates.size(); i++) {
if (target < candidates[i]) continue;
sol.push_back(candidates[i]);
combination(candidates, result, sol, target - candidates[i], i + 1);
sol.pop_back();
}
}

vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> sol;
combination(candidates, ans, sol, target, 0);
return ans;
}
};

posi+1 获得,避免重复使用同一个元素,但是I内说明:without duplicatesII没有说

所以…. 还得手动检查一下 sol 是不是在 result 里边了

第三题

特点

Find all possible combinations of k numbers that add up to a number n
k 个数字组合成 n

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
void combination(vector<vector<int>>& result, vector<int> sol, int k, int n) {
if (sol.size() == k&&n == 0) { result.push_back(sol); return; }
if (sol.size() < k) {
for (int i = sol.empty() ? 1 : sol.back() + 1; i <= 9; i++) {
if (n < i)break;
sol.push_back(i);
combination(result, sol, k, n - i);
sol.pop_back();
}
}
}

vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> ans;
vector<int> sol;
combination(ans, sol, k, n);
return ans;
}
};

边缘条件从 target==0 变成了 sol.size()==k

小结

大致结题步骤

  • 确定边缘条件:比如 sol.size() 或者 target==0 这种
  • 递归条件:push_back 记得要 pop_back,我现在想到自己之前的递归,写的真是…..各种tempback

039.Combination Sum I&amp;II&amp;III

https://iii.run/archives/8afd80cdf910.html

作者

mmmwhy

发布于

2018-09-06

更新于

2021-05-30

许可协议