mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
50 lines
1.8 KiB
HTML
50 lines
1.8 KiB
HTML
<p>Given an array of integers <code>nums</code>, calculate the <strong>pivot index</strong> of this array.</p>
|
|
|
|
<p>The <strong>pivot index</strong> is the index where the sum of all the numbers <strong>strictly</strong> to the left of the index is equal to the sum of all the numbers <strong>strictly</strong> to the index's right.</p>
|
|
|
|
<p>If the index is on the left edge of the array, then the left sum is <code>0</code> because there are no elements to the left. This also applies to the right edge of the array.</p>
|
|
|
|
<p>Return <em>the <strong>leftmost pivot index</strong></em>. If no such index exists, return -1.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [1,7,3,6,5,6]
|
|
<strong>Output:</strong> 3
|
|
<strong>Explanation:</strong>
|
|
The pivot index is 3.
|
|
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
|
|
Right sum = nums[4] + nums[5] = 5 + 6 = 11
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [1,2,3]
|
|
<strong>Output:</strong> -1
|
|
<strong>Explanation:</strong>
|
|
There is no index that satisfies the conditions in the problem statement.</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [2,1,-1]
|
|
<strong>Output:</strong> 0
|
|
<strong>Explanation:</strong>
|
|
The pivot index is 0.
|
|
Left sum = 0 (no elements to the left of index 0)
|
|
Right sum = nums[1] + nums[2] = 1 + -1 = 0
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
|
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong>Note:</strong> This question is the same as 1991: <a href="https://leetcode.com/problems/find-the-middle-index-in-array/" target="_blank">https://leetcode.com/problems/find-the-middle-index-in-array/</a></p>
|