mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 494.cpp
This commit is contained in:
parent
24d1a44579
commit
20e17bc39e
36
LeetCode-CN/494.cpp
Normal file
36
LeetCode-CN/494.cpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
class Solution {
|
||||
public:
|
||||
int findTargetSumWays(vector<int>& nums, int S) {
|
||||
if (S < -1000 || S>1000) return 0;
|
||||
|
||||
int sz = nums.size();
|
||||
int msz = sizeof(int) * 2048;
|
||||
int _bin[2048] = { 0 };
|
||||
int _xbin[2048] = { 0 };
|
||||
int* p = _bin;
|
||||
int* q = _xbin;
|
||||
memset(p, 0, msz);
|
||||
p[1000] = 1;
|
||||
|
||||
for (int i = 0; i < sz; i++)
|
||||
{
|
||||
memset(q, 0, msz);
|
||||
for (int j = 0; j < 2048; j++)
|
||||
{
|
||||
if (p[j])
|
||||
{
|
||||
//printf("p[%d]=%d\n", p[j]);
|
||||
// now=j-1000;
|
||||
int L = j - nums[i]; // now-nums[i]+1000
|
||||
int R = j + nums[i];
|
||||
q[L] += p[j];
|
||||
q[R] += p[j];
|
||||
//printf("q[%d]=%d\n", L, q[L]);
|
||||
//printf("q[%d]=%d\n", R, q[R]);
|
||||
}
|
||||
}
|
||||
std::swap(p, q);
|
||||
}
|
||||
return p[S + 1000];
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user