Getting Runtime Error with the following code. But it runs well in eclipse.


  • 0
    Z

    public class NumArray {

    public int[] nums;
    public List<Integer> allSum = new ArrayList<Integer>();
    public NumArray(int[] nums){
    	int length = nums.length;
    	int tmpSum = 0;
    	for (int i = 0; i < length; i++){
    		tmpSum += nums[i];
    		allSum.add(tmpSum);
    	}
    }
    
    void update(int i, int val){
    	if(i < 0 || i >= nums.length){
    		return;
    	}
    	int tmp = val - nums[i];
    	nums[i] = val;
    	for(; i < nums.length; i++){
    		allSum.set(i, allSum.get(i) + tmp);
    	}
    }
    
    public int sumRange(int i, int j){
    	if(i < 0 || i >= nums.length || j < 0 || j >= nums.length || i > j){
    		return 0;
    	}
    	if(i == 0){
    		return allSum.get(j);
    	}
    	else{
    		return allSum.get(j) - allSum.get(i - 1);
    	}
    }
    

    }
    //Runtime Error
    //Last executed input: [1,3,5],sumRange(0,2),update(1,2),sumRange(0,2)


  • 0
    A

    Why don't you try an array instead of arraylist


Log in to reply
 

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