A good trying for 0-1 package problems. Using dp[i][j] to show the number of methods with ith element in the array and j the sum. The initiative is dp[0][nums[0]] = 1. However, this problem has many things to think of, say the first element 0 can be +0 or -0, and since j could be negative, an offset is used. The state transformation is dp[i][j] = dp[i-1][j-nums[i]] + dp[i-1][j+nums[i]], with offset added. At last, there's still a not predicted error that the array dp should be manual spared spaces. The dp process of this problem is not too complicated, but many details are.
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {
int dp[25][5001];
memset(dp, 0, sizeof(dp));
int ofs = 2000;
bool flag = nums[0];
if(S>1000) return 0;
dp[0][nums[0]+ofs] = 1;
dp[0][-nums[0]+ofs] = 1;
for(int i=1;i<nums.size();i++){
for(int j=-1000;j<=S+1000;j++){
dp[i][j+ofs]=dp[i-1][j-nums[i]+ofs]+dp[i-1][j+nums[i]+ofs];
}
}
if(flag==0) return 2*dp[nums.size()-1][S+ofs];
else return dp[nums.size()-1][S+ofs];
}
};