Simple O(1) time and O(n) space Java solution


  • 0
    K
    public class NumArray {
        int[] leftSums;
        
        public NumArray(int[] nums) {
            leftSums = new int[nums.length + 1];
            
            for(int i = 1; i <= nums.length; ++i)
                leftSums[i] = nums[i-1] + leftSums[i-1];
        }
    
        public int sumRange(int i, int j) {
            return leftSums[j+1] - leftSums[i];
        }
    }

Log in to reply
 

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