1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class NumArray { int sums[]; public NumArray(int[] nums) { if (nums == null || nums.length == 0){ return; } sums = new int[nums.length]; 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 (sums == null) return 0; return i == 0?sums[j]:sums[j]-sums[i-1]; } } |
No comments:
Post a Comment