LeetCode 724. Find Pivot Index

724. Find Pivot Index

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.

Example 2:

Input: 
nums = [1, 2, 3]
Output: -1
Explanation: 
There is no index that satisfies the conditions in the problem statement.

Note:

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


    题意:给定一个数组,按要求在数组中找到一个"pivot index",使得在这个支点左侧数据的和等于右侧数据的和,若不存在,则返回-1;若有多个pivot,则返回最左侧的一个。


    分析:首先看到,这个数组的大小在[0,10000]之间,数组的数据取值范围在[-1000,1000]之间,如果直接用最简单的算法,通过两层循环,依次遍历数组中每个数据的左侧之和与右侧之和,这种算法时间复杂度达到了O(n^2),显然会返回TLE,不可用。

    考虑以空间换时间:我们可以通过两个一维数组sum_of_left[i],sum_of_right[i]分别用于保存nums[i]左侧数据之和与nums[i]右侧数据之和,并且可以知道:

    sum_of_left[i] = sum_of_left[i-1] + nums[i]
    sum_of_right[i] = sum_of_right[i+1] + nums[i]
    因此,判断sum_of_left[i]与sum_of_right[i]是否相等即可判断i是否就是要找的"pivot index"。


    代码实现如下:

    class Solution {
    public:
    	int pivotIndex(vector<int>& nums) {
    		int s = nums.size();
    		if (s <= 2)
    			return -1;
    		int sum_of_left[10001] = { 0 };
    		int sum_of_right[10001] = { 0 };
    
    		sum_of_left[0] = nums[0];
    		sum_of_right[s-1] = nums[s-1];
    		for (int i = 1; i < s; i++) {
    			sum_of_left[i] = sum_of_left[i - 1] + nums[i];
    		}
    		for (int j = s - 2; j >= 0; j--) {
    			sum_of_right[j] = sum_of_right[j + 1] + nums[j];
    		}
    
    		int index = -1;
    		for (int k = 0; k < s; k++) {
    			if (sum_of_left[k] == sum_of_right[k]) {
    				index = k;
    				break;
    			}
    		}
    		return index;
    	}
    };

    提交之后,可以看到该算法被Accepted,但是用时53ms,因为其最坏情况下,也需要运行O(3*10000*10000)。


    算法2:另一种思路可以是,先计算整个数组的和Sum,遍历数组时,每次更新curSum,curSum代表当前nums[i]左侧数据之和,如果nums[i]为pivot,则根据:

    sum(nums[0,1,...,i-1]) = Sum - sum(nums[i+1, ..., n]) - nums[i] 

    必然有:
curSum = Sum - curSum - nums[i] 

代码实现如下:
class Solution {
public:
	int pivotIndex(vector<int>& nums) {
		int s = nums.size();
		int sum = accumulate(nums.begin(), nums.end(),0);

		int curSum = 0;
		for (int i = 0; i < s; i++) {
			if (curSum == sum - curSum - nums[i])
				return i;
			curSum += nums[i];
		}
		return -1;
	}
};

附录:
accumulate函数:用来计算特定范围内(包括连续的部分和初始值)所有元素的和,其头文件在numeric中。
计算示例:可通过accumulate函数来计算和与乘积:
#include <numeric>
#include<iostream>
#include<vector>

using namespace std;
int main() {
	vector<int> v;
	for (int i = 1; i <= 10; i++)
		v.push_back(i);
	cout << "sum: " << accumulate(v.begin(), v.end(), 0) << endl;
	cout << "multiply: " << accumulate(v.begin(), v.end(), 1, multiplies<int>()) << endl;
}
详细介绍可见: C++ STL算法之accumulate函数





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值