JAVA-----------Easy Version To Understand!!!!!!!!!!!


  • 6
    H
        int[] nums;
    int[] sum;
    
    public NumArray(int[] nums) {
        if(nums==null||nums.length==0)
        return;
    	this.nums = nums;
    	int len = nums.length;
    	sum = new int[len];
    	sum[0] = nums[0];
    	for (int i = 1; i < len; i++)
    		sum[i] = sum[i - 1] + nums[i];
    }
    
    public int sumRange(int i, int j) {
    
    	return sum[j] - sum[i] + nums[i];
    }

Log in to reply
 

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