Given an array of integers nums, the array element from the sum of the index i to j within the range of elements (i ≤ j), comprising i, j points.
Example:
Given nums = [-2, 0, 3, -5, 2, -1], the summation function sumRange ()
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
说明:
You can assume that the array can not be changed.
Calls sumRange method multiple times
1 public class T303 { 2 private int[] sum; 3 public T303(int[] nums) { 4 sum = new int[nums.length + 1]; 5 for (int i = 0; i < nums.length; i++) { 6 sum[i] = sum[i - 1] + nums[i]; 7 } 8 } 9 10 public int sumRange(int i, int j) { 11 return sum[j + 1] - sum[i]; 12 } 13 }