Java solution using pointers


  • 0
    S
    class NumArray {
        int nums[];
        public NumArray(int[] nums) {
            this.nums = nums;
        }
        
        public void update(int i, int val) {
            nums[i]=val;
        }
        
        public int sumRange(int i, int j) {
            int sum = 0;
            int p=i;
            while(p<=j){
                if(p!=j){
                sum+=nums[p++];
                sum+=nums[j--];
                }else sum+=nums[p++];
            }
            return sum;
        }
    }
    

Log in to reply
 

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