1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-26 02:00:27 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/目标和 [target-sum].html
2022-03-29 12:43:11 +08:00

43 lines
1.3 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>target</code></p>
<p>向数组中的每个整数前添加 <code>'+'</code><code>'-'</code> ,然后串联起所有整数,可以构造一个 <strong>表达式</strong> </p>
<ul>
<li>例如,<code>nums = [2, 1]</code> ,可以在 <code>2</code> 之前添加 <code>'+'</code> ,在 <code>1</code> 之前添加 <code>'-'</code> ,然后串联起来得到表达式 <code>"+2-1"</code></li>
</ul>
<p>返回可以通过上述方法构造的、运算结果等于 <code>target</code> 的不同 <strong>表达式</strong> 的数目。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,1,1,1,1], target = 3
<strong>输出:</strong>5
<strong>解释:</strong>一共有 5 种方法让最终目标和为 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>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1], target = 1
<strong>输出:</strong>1
</pre>
<p> </p>
<p><strong>提示:</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>