【LeetCode刷题】- 寻找数组的中心索引

题目:

【LeetCode刷题】- 寻找数组的中心索引_第1张图片

代码

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

提交截图:

【LeetCode刷题】- 寻找数组的中心索引_第2张图片

分析总结:

(周末统一完成)

你可能感兴趣的:(leetCode,C/C++学习,LeetCode刷题系列)