Java solution easy to understand


  • 0
    C

    class Solution {
    public int pivotIndex(int[] nums) {
    if(nums == null || nums.length == 0) return -1;
    int len = nums.length;
    int sum[] = new int[len + 1];
    sum[0] = 0;
    for(int i = 1; i <= len; i++){
    sum[i] = sum[i - 1] + nums[i-1];
    }
    int res = -1;
    for(int i = 0; i < len; i++){
    if(sum[i] == sum[len] - sum[i+1]){
    res = i;
    break;
    }
    }
    return res;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.