-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindTargetSumWays.cpp
More file actions
26 lines (25 loc) · 943 Bytes
/
findTargetSumWays.cpp
File metadata and controls
26 lines (25 loc) · 943 Bytes
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
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int target) {
int sum = std::accumulate(nums.begin(), nums.end(), 0), neg = sum - target;
if (neg < 0 || neg % 2 != 0) return 0;
int n = nums.size();
neg /= 2;
vector<vector<int> > dp(n + 1, vector<int>(neg + 1, 0));
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
int num = nums[i - 1];
for (int j = 0; j <= neg; j++) {
dp[i][j] = dp[i - 1][j];
if (j >= num) {
dp[i][j] += dp[i - 1][j - num];
}
}
}
return dp[n][neg];
}
};
// 作者:LeetCode-Solution
// 链接:https://leetcode-cn.com/problems/target-sum/solution/mu-biao-he-by-leetcode-solution-o0cp/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。