Skip to content
本页目录

题目描述

https://leetcode.cn/problems/find-pivot-index

给你一个整数数组 nums ,请计算数组的 中心下标 。
数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等于右侧所有元素相加的和。
如果中心下标位于数组最左端,那么左侧数之和视为 0 ,因为在下标的左侧不存在元素。这一点对于中心下标位于数组最右端同样适用。
如果数组有多个中心下标,应该返回 最靠近左边 的那一个。如果数组不存在中心下标,返回 -1 。

示例 1:

输入:nums = [1, 7, 3, 6, 5, 6]
输出:3
解释:
中心下标是 3 。
左侧数之和 sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 ,
右侧数之和 sum = nums[4] + nums[5] = 5 + 6 = 11 ,二者相等。

示例 2:

输入:nums = [1, 2, 3]
输出:-1
解释:
数组中不存在满足此条件的中心下标。

示例 3:

输入:nums = [2, 1, -1]
输出:0
解释:
中心下标是 0 。
左侧数之和 sum = 0 ,(下标 0 左侧不存在元素),
右侧数之和 sum = nums[1] + nums[2] = 1 + -1 = 0 。

提示:

  • 1 <= nums.length <= 104
  • -1000 <= nums[i] <= 1000

注意:本题与主站 1991 题相同:https://leetcode-cn.com/problems/find-the-middle-index-in-array/

思路:两趟遍历

从左往右累加,从右往左累加,最后比较结果

csharp
public class Solution {
    public int PivotIndex(int[] nums) {
        int n = nums.Length;
        int[] leftSum = new int[n];
        leftSum[0] = 0;
        for(int i=1; i<nums.Length; i++){
            leftSum[i] = leftSum[i-1] + nums[i-1];
        }
        int[] rightSum = new int[n];
        rightSum[n-1] = 0;
        for(int i=n-2; i>=0; i--){
            rightSum[i] = rightSum[i+1] + nums[i+1];
        }
        //循环比较
        for(int i=0; i<n; i++){
            if(leftSum[i] == rightSum[i]){
                return i;
            }
        }
        return -1;
    }
}

思路:一趟扫描【官方】

求总和,然后从左边求取前缀和,当 preSum + nums[i] + preSum 等于总和的时候就成立,所以判断 preSum * 2 + nums[i] == total

csharp
public class Solution {
    public int FindMiddleIndex(int[] nums) {
        int total = nums.Sum();
        int sum = 0;
        for (int i = 0; i < nums.Length; ++i) {
            if (2 * sum + nums[i] == total) {
                return i;
            }
            sum += nums[i];
        }
        return -1;
    }
}

Released under the MIT License.