303.Range Sum Query - Immutable Posted on 2017-09-13 | In LeetCode Solution 1: TLENo DP bro. 1234567891011121314public class NumArray { int[] nums; public NumArray(int[] nums) { this.nums = nums; } public int sumRange(int i, int j) { int sum = 0; for(int k = i; k <= j; k++) { sum += nums[k]; } return sum; }} Solution 2: accepted 198msDP.Time: O(n)Space: O(1) (no extra) 12345678910111213public class NumArray { int[] nums; public NumArray(int[] nums) { this.nums = nums; for(int i = 1; i < nums.length; i++) { nums[i] += nums[i-1]; } } public int sumRange(int i, int j) { return (i == 0)? nums[j] : nums[j] - nums[i-1]; }}