3ms ,Java solution with protect element


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

Log in to reply
 

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