Leetcode724—Find the central index of an array

Title description:

Given an array of integer types nums, write a method that returns the "center index" of the array.

We define the array center index as follows: 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 there is no central index in the array, then we should return -1. If the array has multiple central indexes, then we should return the one closest to the left.

Example 1:

Input: nums = [1, 7, 3, 6, 5, 6]

Output: 3
Explanation: 

The sum of the left numbers of index 3 (nums [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 that meets this condition in the array.

 

Explanation:

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


At first glance, the title seems to be quite simple. It is not enough to use a double pointer, so I use two pointers to sum from the front and the back, and move forward relatively small.

int pivotIndex(vector<int>& nums) {
        if(!nums.size())
        {
            return -1;
        }
        int i = 0;
	    int j = nums.size() - 1;
	    int firstSum = nums[i];
	    int lastSum = nums[j];
	    while (i < j)
	    {
		    if (firstSum < lastSum)
		    {
			    firstSum += nums[++i];
	        }
		    else
	    	{
			    lastSum += nums[--j];
		    }
	    }
	    if (lastSum == firstSum)
	    {
	    	return i;
	    }
        else
        {
            return -1;
        }

    }

However, the chestnuts of {-1, -1, -1, -1, -1,0} appeared during the test, which is not so simple ~ change the way of thinking, since it is a search, use a hash table?

int pivotIndex(vector<int>& nums) {
        if(!nums.size())
        {
            return -1;
        }
        map<int, int> sum1;
	    map<int, int> sum2;
	    int firstSum = 0;
	    int lastSum = 0;
	    for (int i = 0; i < nums.size(); i++)
	    {
		    firstSum += nums[i];
		    sum1[i] = firstSum;
	    }
	    for (int i = nums.size() - 1; i >= 0; i--)
	    {
		    lastSum += nums[i];
		    sum2[i] = lastSum;
	    }
        for (int i = 0; i < nums.size(); i++)
	    {
		    if (sum1[i] == sum2[i])
		    {
			    return i;
		    }
	    }
	return -1;
};

Passed, but "memory consumption: 26.4 MB, defeated 5.01% of users in all cpp submissions", in fact, this storage is linear, there is no need to use a hash table, just use an array, memory consumption will drop Quite a lot.

Last, let's take a look at the code written by the big guys. It is a very magical idea. Since the summation on both sides is the same, the number between the side and * 2 + is the sum of the entire array.

 int pivotIndex(vector<int>& nums) 
    {
        int sum = 0;
        int sumleft = 0;
        int len = size(nums);
        for(int i=0;i<len;i++)
            sum += nums[i];
        for(int i=0;i<len;i++)
        {
            if(sumleft*2+nums[i]==sum)
                return i;
            sumleft+=nums[i];
        }
        return -1;
    }

 

Published 17 original articles · liked 0 · views 3229

Guess you like

Origin blog.csdn.net/qq_31874075/article/details/103433652