思路
计算前缀和
class NumArray {
private int[] sum;
public NumArray(int[] nums) {
sum = new int[nums.length+1];
//计算前缀和
for(int i=0;i<nums.length;i++)
sum[i+1] = sum[i]+nums[i];
}
public int sumRange(int i, int j) {
//用前缀和进行求区间和
return sum[j+1]-sum[i];
}
}