线段树 区域和检索 - 数组不可变(领扣)

class NumArray {
    private int[] sums;
    public NumArray(int[] nums) {
        sums = new int[nums.length];
        if (nums.length == 0) {
            return;
        }
        sums[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            sums[i] += sums[i - 1] + nums[i];
        }
    }
    
    public int sumRange(int i, int j) {
        if (i == 0) {
            return sums[j];
        } else {
            return sums[j] - sums[i - 1];
        }  
    }
}

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(i,j);
 */

相关文章:

  • 2022-03-03
  • 2021-05-13
  • 2021-09-24
  • 2021-07-14
  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
  • 2021-07-31
猜你喜欢
  • 2021-12-06
  • 2022-01-06
  • 2021-12-29
  • 2021-05-14
  • 2022-03-05
  • 2022-12-23
相关资源
相似解决方案