Easy Java Solution


  • 0
    K
    class Solution {
        public int pivotIndex(int[] nums) {
            int m= nums.length;
            if(m==0) return -1;
            for(int i=1;i<m;i++) nums[i]+=nums[i-1];
            if(0==nums[m-1]-nums[0]) return 0;
            for(int i=1;i<m-1;i++){
                if(nums[i-1]== nums[m-1]-nums[i]) return i;
            }
            if(nums[m-2]==0) return m-1;
            return -1;
        }
    }
    

Log in to reply
 

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