寻找数组的中心索引 Find Pivot Index

目录

  • 问题描述
  • 1. 分别计算左和与右和
    • C++
  • 2. 仅计算左和(包含当前元素)
    • C++
  • 3. 前缀和
    • C++
    • Python

问题描述

Given an array of integers nums, write a method that returns the “pivot” index of this array.

We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

Example 1:
Input:
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation:
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.

  • mode: easy
  • tag: array

1. 分别计算左和与右和

分别计算数组每个位置的左边与右边的和(不包含当前元素),然后判断每个位置左和是否等于右和。左和、右和都存在向量中,所以内存消耗大。

C++

class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        if(nums.size()==0)
            return -1;
        int nums_size=nums.size();
        vector<int> left_sum(nums_size);
        vector<int> right_sum(nums_size);
        
        left_sum[0]=0;
        for(int i=1;i<nums.size();i++)
        {
            left_sum[i] = left_sum[i-1] + nums[i-1];
        }
        
        right_sum[nums_size-1]=0;
        for(int i=nums_size-2; i>=0;--i)
        {
            right_sum[i] = right_sum[i+1]+nums[i+1];
        }
        
        for(int i=0;i<nums_size;i++)
        {
            if(left_sum[i]==right_sum[i])
                return i;
        }
        
        return -1;
    }
};

执行用时 : 56 ms, 在所有 C++ 提交中击败了25.92%的用户
内存消耗 :31.1 MB, 在所有 C++ 提交中击败了6.67%的用户

可以看出算法仍需改进。左和与右和重复计算,可以考虑计算一遍。

2. 仅计算左和(包含当前元素)

将数组每个位置的左边数值的和存在一个向量中,内存消耗较大。

  • 时间复杂度: O ( N ) O(N) O(N)
  • 空间复杂度: O ( N ) O(N) O(N)

C++

class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        if(nums.size()==0)
            return -1;
        int nums_size=nums.size();
        vector<int> left_sum(nums_size);
        
        left_sum[0]=nums[0];
        for(int i=1;i<nums.size();i++)
        {
            left_sum[i] = left_sum[i-1] + nums[i];
        }
        for(int i=0;i<nums_size;i++)
        {
            int left=(i==0)?0:left_sum[i-1];
            int right=left_sum[nums_size-1]-left_sum[i];
            if(left==right)
                return i;
        }        
        return -1;
    }
};

执行用时 : 44 ms , 在所有 C++ 提交中击败了 50.03% 的用户
内存消耗 : 30.6 1MB, 在所有 C++ 提交中击败了6.67%的用户

分析:相比上一方法减少了计算,但是内存消耗依然较大。

3. 前缀和

假设存在中心索引,那么有和:S = leftsum+nums[i]+rightsum,其中leftsum==rightsum。
那么:leftsum = S - nums[i] - rightsum 可得:2*leftsum = S -nums[i]。

  • 时间复杂度: O ( N ) O(N) O(N)
  • 空间复杂度: O ( 1 ) O(1) O(1)

C++

class Solution {
public:
    int pivotIndex(vector<int>& nums) {
            int sumLeft=0;//索引左边数组元素的和
            int sumTotal=0;//数组所有元素的和
        for(int i=0;i<nums.size();i++)
            sumTotal+=nums[i];
        for(int j=0;j<nums.size();j++)
        {
          if(sumLeft*2==sumTotal-nums[j])
              return j;

            sumLeft+=nums[j];
        }
        return -1;
    }
};

执行用时 : 48 ms, 在所有 C++ 提交中击败了40.60%的用户
内存消耗 :30.1 MB, 在所有 C++ 提交中击败了6.67%的用户

Python

class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        if(len(nums)==0):
            return -1
        total_sum=sum(nums)
        left_sum=0
        for i in range(len(nums)):
            if left_sum*2==total_sum-nums[i]:
                return i
            else:
                left_sum+=nums[i]
        return -1

你可能感兴趣的:(leetcode)