1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/生成平衡数组的方案数 [ways-to-make-a-fair-array].html
2022-03-29 12:43:11 +08:00

54 lines
2.1 KiB
HTML
Raw Permalink 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> 。你需要选择 <strong>恰好</strong> 一个下标(下标从 <strong>0</strong> 开始)并删除对应的元素。请注意剩下元素的下标可能会因为删除操作而发生改变。</p>
<p>比方说,如果 <code>nums = [6,1,7,4,1]</code> ,那么:</p>
<ul>
<li>选择删除下标 <code>1</code> ,剩下的数组为 <code>nums = [6,7,4,1]</code> 。</li>
<li>选择删除下标 <code>2</code> ,剩下的数组为 <code>nums = [6,1,4,1]</code> 。</li>
<li>选择删除下标 <code>4</code> ,剩下的数组为 <code>nums = [6,1,7,4]</code> 。</li>
</ul>
<p>如果一个数组满足奇数下标元素的和与偶数下标元素的和相等,该数组就是一个 <strong>平衡数组</strong></p>
<p>请你返回删除操作后,剩下的数组<em> </em><code>nums</code><em> </em>是 <strong>平衡数组</strong> 的 <strong>方案数</strong> 。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [2,1,6,4]
<b>输出:</b>1
<strong>解释:</strong>
删除下标 0 [1,6,4] -> 偶数元素下标为1 + 4 = 5 。奇数元素下标为6 。不平衡。
删除下标 1 [2,6,4] -> 偶数元素下标为2 + 4 = 6 。奇数元素下标为6 。平衡。
删除下标 2 [2,1,4] -> 偶数元素下标为2 + 4 = 6 。奇数元素下标为1 。不平衡。
删除下标 3 [2,1,6] -> 偶数元素下标为2 + 6 = 8 。奇数元素下标为1 。不平衡。
只有一种让剩余数组成为平衡数组的方案。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [1,1,1]
<b>输出:</b>3
<b>解释:</b>你可以删除任意元素,剩余数组都是平衡数组。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>nums = [1,2,3]
<b>输出:</b>0
<b>解释:</b>不管删除哪个元素,剩下数组都不是平衡数组。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
</ul>