mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
46 lines
1.8 KiB
HTML
46 lines
1.8 KiB
HTML
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, find the <strong>leftmost</strong> <code>middleIndex</code> (i.e., the smallest amongst all the possible ones).</p>
|
|
|
|
<p>A <code>middleIndex</code> is an index where <code>nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1]</code>.</p>
|
|
|
|
<p>If <code>middleIndex == 0</code>, the left side sum is considered to be <code>0</code>. Similarly, if <code>middleIndex == nums.length - 1</code>, the right side sum is considered to be <code>0</code>.</p>
|
|
|
|
<p>Return <em>the <strong>leftmost</strong> </em><code>middleIndex</code><em> that satisfies the condition, or </em><code>-1</code><em> if there is no such index</em>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [2,3,-1,<u>8</u>,4]
|
|
<strong>Output:</strong> 3
|
|
<strong>Explanation:</strong> The sum of the numbers before index 3 is: 2 + 3 + -1 = 4
|
|
The sum of the numbers after index 3 is: 4 = 4
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [1,-1,<u>4</u>]
|
|
<strong>Output:</strong> 2
|
|
<strong>Explanation:</strong> The sum of the numbers before index 2 is: 1 + -1 = 0
|
|
The sum of the numbers after index 2 is: 0
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [2,5]
|
|
<strong>Output:</strong> -1
|
|
<strong>Explanation:</strong> There is no valid middleIndex.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= nums.length <= 100</code></li>
|
|
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong>Note:</strong> This question is the same as 724: <a href="https://leetcode.com/problems/find-pivot-index/" target="_blank">https://leetcode.com/problems/find-pivot-index/</a></p>
|