Concise Java


  • 0
    class Solution {
        public int pivotIndex(int[] nums) {
            if (nums == null || nums.length < 2) return -1;
            int total = 0;
            for (int num : nums) total += num;
            int sum = 0;
            for (int i = 0; i < nums.length; i++) {
                if (total - nums[i] - sum == sum) return i;
                sum += nums[i];
            }
            return -1;
        }
    }
    

Log in to reply
 

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