Version #1 PrefixSum
class NumArray {
// prefixSum i -> sum[0, i)
private int[] prefixSum;
public NumArray(int[] nums) {
this.prefixSum = new int[nums.length + 1];
for (int i = 1; i <= nums.length; i++) {
prefixSum[i] = nums[i - 1] + prefixSum[i - 1];
}
}
public int sumRange(int i, int j) {
return prefixSum[j + 1] - prefixSum[i];
}
}
No comments:
Post a Comment