image.png

解决思路

image.png

  1. class NumArray {
  2. private int[] sum;
  3. public NumArray(int[] nums) {
  4. sum = new int[nums.length + 1];
  5. for (int i = 0; i < nums.length; i++) {
  6. sum[i + 1] = sum[i] + nums[i];
  7. }
  8. }
  9. public int sumRange(int i, int j) {
  10. return sum[j + 1] - sum[i];
  11. }
  12. }