[leetcode] 1. Array – find the central index of the array

Time:2021-9-26

subject

Given an array num of integer type, please write a method that can return the “central index” of the array.
We define the array center index in this way: the sum of all elements on the left of the array center index is equal to the sum of all elements on the right.
If the array does not have a central index, we should return – 1. If the array has multiple central indexes, we should return the one closest to the left.
Degree of difficulty:easy
Example 1:

Input:
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation:
The sum of the left numbers of index 3 (Num [3] = 6) (1 + 7 + 3 = 11) is equal to the sum of the right numbers (5 + 6 = 11).
At the same time, 3 is also the first central index that meets the requirements.

Example 2:

Input:
nums = [1, 2, 3]
Output: – 1
Explanation:
There is no central index in the array that meets this condition.
explain:

The length range of nums is [0, 10000].
Any num [i] will be an integer in the range [- 1000, 1000].

Problem solution

Solution I

analysis

Open up a new array prev_ Sum, the array size is n + 1 (n is the original array size), traverse the entire array, calculate the sum of the first I elements and store it in prev_ In sum [i], pay attention to prev_ sum[0]=0。
Then, the sum of the elements on the left of the original array index J is prev_ Sum [J], the sum of the elements on the right is prev_ sum[N] – prev_ sum[j + 1]。
Cyclic comparison prev_ Is sum [J] related to prev_ sum[N] – prev_ Sum [J + 1] is equal.
Time complexity:O(N)
Spatial complexity:O(N)

code

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

Solution II

analysis

In solution 1, we need to open up a new array. How to avoid the waste of this part of space? Through analysis, we can know that the sum of all elements of the whole array is the sum of the elements on index I plus the left and right sub arrays. According to the definition of the central index, the sum of all elements on the left side of the central index I is left_ Sum is equal to the sum of all the elements on the right_ Sum, i.e. left_ sum * 2 = sum – nums[i]。
Time complexity:O(N)
Spatial complexity:O(1)

code

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

[leetcode] 1. Array - find the central index of the array

Recommended Today

Seven Python code review tools recommended

althoughPythonLanguage is one of the most flexible development languages at present, but developers often abuse its flexibility and even violate relevant standards. So PythoncodeThe following common quality problems often occur: Some unused modules have been imported Function is missing arguments in various calls The appropriate format indentation is missing Missing appropriate spaces before and after […]