<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>

<p>You want to build an <strong>expression</strong> out of nums by adding one of the symbols <code>&#39;+&#39;</code> and <code>&#39;-&#39;</code> before each integer in nums and then concatenate all the integers.</p>

<ul>
	<li>For example, if <code>nums = [2, 1]</code>, you can add a <code>&#39;+&#39;</code> before <code>2</code> and a <code>&#39;-&#39;</code> before <code>1</code> and concatenate them to build the expression <code>&quot;+2-1&quot;</code>.</li>
</ul>

<p>Return the number of different <strong>expressions</strong> that you can build, which evaluates to <code>target</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1,1,1], target = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> There are 5 ways to assign symbols to make the sum of nums be target 3.
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1], target = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 20</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
	<li><code>0 &lt;= sum(nums[i]) &lt;= 1000</code></li>
	<li><code>-1000 &lt;= target &lt;= 1000</code></li>
</ul>

class Solution {
public:
   int solverec(vector<int>& nums,int sum,int index,int target){
       if(index<0){
           if(sum==target){
              return 1;
           }
           else{
               return 0;
           }
       }
       int pos=solverec(nums,sum+nums[index],index-1,target);
       int neg=solverec(nums,sum-nums[index],index-1,target);
      return pos+neg;
   }

   int solvemem(int index,int target,vector<int>& nums,vector<vector<int>> dp){
       if (index==0) {
            if (target==0 && nums[index]==0) return 2;
            else if (nums[index]==target || target==0) return 1;
            else{
                 return 0;
            }
           
        }
        // yoyo dp
        if(dp[index][target]!=-1) return dp[index][target];
        // Pick case.
        int Pick=0;
        if (nums[index]<=target) Pick=solvemem(index-1, target-nums[index], nums, dp);
        // not Pick case
        int notPick=solvemem(index-1, target, nums, dp);

        return dp[index][target] = Pick+notPick;
   }
   int solvemem(int index,int total,vector<int> &nums,int target,unordered_map<long,int> &dp){
        if(index==nums.size()){
            return total==target?1:0;
        }
        long key=static_cast<long>(index)<<32 | (total & 0xffffffffL);
        if(dp.find(key)!=dp.end()) return dp[key];
        int ans=solvemem(index+1,total+nums[index],nums,target,dp)+
        solvemem(index+1,total-nums[index],nums,target,dp);
        dp[key]=ans;
        return ans;
    }
 
    int findTargetSumWays(vector<int>& nums, int target) {
      
    //     int n=nums.size();
       
    //      int sum = accumulate(nums.begin(), nums.end(), 0);
    //     //  return  solverec(nums,0,nums.size()-1,target);
    //    int k=(sum-target);
    //     if (k<0 || k%2!=0){
    //         return 0;
    //     }
    //     vector<vector<int>> dp(n, vector<int> (k+1, -1));
    //     return solvemem(n-1, k/2, nums, dp);
    unordered_map<long,int> dp;
        return solvemem(0,0,nums,target,dp);
    }
};
64963