THX for sharing.

Actually, there is no need to allocate a new array in each loop, can be done with two arrays and just swap them after inner loop ends.

Here is my C++ version.

class Solution { public: int findTargetSumWays(vector<int>& nums, int S) { int sum = 0; for(auto x: nums) sum += x; if(sum < S || -sum > S) return 0; vector<int>cur(2 * sum + 1); vector<int>next(2 * sum + 1); cur[sum] = 1; for(int i = 0; i < nums.size(); i++){ for(int j = 0; j < 2 * sum + 1; j++){ if(cur[j] != 0){ next[j + nums[i]] += cur[j]; next[j - nums[i]] += cur[j]; cur[j] = 0; } } swap(cur, next); } return cur[sum + S]; } };