@@ -0,0 +1,1036 @@
+/*%0AGiven an integer array nums, find the sum of the elements between indices i and j (i %E2%89%A4 j), inclusive.%0A%0AExample:%0A%0AGiven nums = %5B-2, 0, 3, -5, 2, -1%5D%0A%0AsumRange(0, 2) -%3E 1%0AsumRange(2, 5) -%3E -1%0AsumRange(0, 5) -%3E -3%0A%0ANote:%0A%0A    You may assume that the array does not change.%0A    There are many calls to sumRange function.%0A%0A*/%0A%0A%0Apublic class RangeSumQueryImmutable %7B%0A%09public static class NumArray %7B%0A%09%09private int%5B%5D ar;%0A%09%09public NumArray(int%5B%5D nums) %7B%0A%09%09%09ar = nums;%0A%09%09%09for(int i = 1; i %3C nums.length; i++)%7B%0A%09%09%09%09ar%5Bi%5D += ar%5Bi-1%5D;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09public int sumRange(int i, int j) %7B%0A%09%09%09return i==0? ar%5Bj%5D: ar%5Bj%5D - ar%5Bi-1%5D;%0A%09%09%7D%0A%09%7D%0A%0A%09public static void main(String%5B%5D args) %7B%0A%09%09int%5B%5D nums = new int%5B%5D%7B-2, 0, 3, -5, 2, -1%7D;%0A%09%09System.out.print( %22Before: %22);%0A%09%09for(int i = 0; i %3C nums.length; i++)%7B%0A%09%09%09System.out.print(nums%5Bi%5D + %22 %22);%0A%09%09%7D%0A%09%09System.out.println();%0A%09%09NumArray a = new NumArray(nums);%0A%09%09System.out.println(%22Sum between 0 and 5 is: %22 +a.sumRange(0, 5));%0A%09%09System.out.println(%22Sum between 2 and 4 is: %22 +a.sumRange(2, 4));%0A%09%09return;%0A%09%7D%09%0A%7D%0A

range sum immutable