Skip to content

Commit a072c04

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 303_Range_Sum_Query_-_Immutable.java
1 parent eb14adf commit a072c04

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class NumArray {
2+
private int[] dp;
3+
4+
public NumArray(int[] nums) {
5+
if (nums == null || nums.length == 0) {
6+
return;
7+
}
8+
9+
dp = new int[nums.length];
10+
dp[0] = nums[0];
11+
12+
for (int i = 1; i < nums.length; i++) {
13+
dp[i] = dp[i - 1] + nums[i];
14+
}
15+
}
16+
17+
public int sumRange(int i, int j) {
18+
if (i == 0) {
19+
return dp[j];
20+
}
21+
22+
return dp[j] - dp[i - 1];
23+
}
24+
}

0 commit comments

Comments
 (0)