Leetcode / 724. Find Pivot Index
Pick a programming language:
Here is the source code for the solution to this problem.
class Solution {
    public int pivotIndex(int[] nums) {
        int totalSum = 0;
        int leftSum = 0;
        for (int i = 0; i < nums.length; i++) {
            totalSum += nums[i];
        }
        for (int i = 0; i < nums.length; i++) {
            if (leftSum == totalSum - nums[i] - leftSum) {
                return i;
            }
            leftSum += nums[i];
        }
        return -1;
    }
}
Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: