@@ -0,0 +1,1133 @@
+package com.pengyifan.leetcode;%0A%0A/**%0A * Given an integer array nums, find the sum of the elements between indices i and j (i %E2%89%A4 j),%0A * inclusive.%0A * %3Cp%3E%0A * Example:%0A * Given nums = %5B-2, 0, 3, -5, 2, -1%5D%0A * %3Cpre%3E%0A * sumRange(0, 2) -%3E 1%0A * sumRange(2, 5) -%3E -1%0A * sumRange(0, 5) -%3E -3%0A * %3C/pre%3E%0A * Note:%0A * You may assume that the array does not change.%0A * There are many calls to sumRange function.%0A */%0Apublic class RangeSumQueryImmutable %7B%0A%0A  private int%5B%5D sum;%0A%0A  public RangeSumQueryImmutable(int%5B%5D nums) %7B%0A    sum = new int%5Bnums.length%5D;%0A    if (nums.length %3E 0) %7B%0A      sum%5B0%5D = nums%5B0%5D;%0A      for (int i = 1; i %3C nums.length; i++) %7B%0A        sum%5Bi%5D = sum%5Bi - 1%5D + nums%5Bi%5D;%0A      %7D%0A    %7D%0A  %7D%0A%0A  public int sumRange(int i, int j) %7B%0A    if (i %3C 0 %7C%7C i %3E= sum.length %7C%7C j %3C 0 %7C%7C j %3E= sum.length) %7B%0A      throw new Error();%0A    %7D%0A    if (i == 0) %7B%0A      return sum%5Bj%5D;%0A    %7D else %7B%0A      return sum%5Bj%5D - sum%5Bi - 1%5D;%0A    %7D%0A  %7D%0A%7D%0A%0A// Your RangeSumQueryImmutable object will be instantiated and called as such:%0A// RangeSumQueryImmutable numArray = new RangeSumQueryImmutable(nums);%0A// numArray.sumRange(0, 1);%0A// numArray.sumRange(1, 2);%0A

Range Sum Query - Immutable