-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL494.cpp
39 lines (39 loc) · 843 Bytes
/
L494.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
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
int findTargetSumWays(vector<int> &nums, int target)
{
int sum = 0;
for (int x : nums)
{
sum += x;
}
int V = (sum + target) / 2, N = nums.size();
if (target > sum)
return 0;
if (target < 0 - sum)
return 0;
if ((sum + target) % 2 == 1)
return 0;
vector<int> res(20010, 0);
res[0] = 1;
for (int i = 0; i < N; i++)
{
for (int j = V; j >= nums[i]; j--)
{
res[j] += res[j - nums[i]];
}
}
return res[V];
}
};
int main()
{
vector<int> myvec = {1, 1, 1, 1, 1};
Solution sol;
cout << sol.findTargetSumWays(myvec, 5);
return 0;
}