Package g0401_0500.s0494_target_sum
Class Solution
java.lang.Object
g0401_0500.s0494_target_sum.Solution
494 - Target Sum.<p>Medium</p>
<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>'+'</code> and <code>'-'</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>'+'</code> before <code>2</code> and a <code>'-'</code> before <code>1</code> and concatenate them to build the expression <code>"+2-1"</code>.</li>
</ul>
<p>Return the number of different <strong>expressions</strong> that you can build, which evaluates to <code>target</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [1,1,1,1,1], target = 3</p>
<p><strong>Output:</strong> 5</p>
<p><strong>Explanation:</strong></p>
<pre><code> 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
</code></pre>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [1], target = 1</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 20</code></li>
<li><code>0 <= nums[i] <= 1000</code></li>
<li><code>0 <= sum(nums[i]) <= 1000</code></li>
<li><code>-1000 <= target <= 1000</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
findTargetSumWays
public int findTargetSumWays(int[] nums, int s)
-