-
Notifications
You must be signed in to change notification settings - Fork 1
/
416+Partition Equal Subset Sum.cpp
59 lines (44 loc) · 1.8 KB
/
416+Partition Equal Subset Sum.cpp
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
// 暴力
class Solution {
public:
bool dfs(int i, int remain, vector<int>& nums) {
if (remain == 0) return true;
// target为负数 或 nums数组遍历完 返回false
if (remain < 0 || i == nums.size()) return false;
// 选这个数字 减这个数字
bool b1 = dfs(i+1, remain - nums[i], nums);
// 不选这个数字 不减这个数字
bool b2 = dfs(i+1, remain, nums);
return b1 || b2; //注意这里是 || 有一条路可以满足就行
}
bool canPartition(vector<int>& nums) {
int sumn = accumulate(nums.begin(), nums.end(), 0);
if (sumn & 1) return false;
return dfs(0, sumn / 2, nums);
}
};
//记忆化搜索
class Solution {
public:
bool dfs(int i, int remain, vector<int>& nums, vector<vector<int>> &memo) {
if (remain == 0) return true;
if (remain < 0 || i == nums.size()) return false;
// 记忆化
if (memo[i][remain] != -1) return memo[i][remain];
memo[i][remain] = dfs(i+1, remain - nums[i], nums, memo) || dfs(i+1, remain, nums, memo); //注意这里是 || 有一条路可以满足就行
/*
// 选这个数字 减这个数字
bool b1 = dfs(i+1, remain - nums[i], nums)
// 不选这个数字 不减这个数字
bool b2 = dfs(i+1, remain, nums);
*/
return memo[i][remain];
}
bool canPartition(vector<int>& nums) {
int sumn = accumulate(nums.begin(), nums.end(), 0);
if (sumn & 1) return false;
int target = sumn / 2;
vector<vector<int>> memo(nums.size(), vector<int>(target + 1, -1)); //memo[i][j]里存出现过,就直接返回它的值
return dfs(0, target, nums, memo);
}
};