Skip to content

Commit 4aea9f7

Browse files
committed
add: SumBetweenRange
1 parent 5ef5c80 commit 4aea9f7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

java/Prefix Sums/SumBetweenRange.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class SumBetweenRange {
2+
int[] prefixSum;
3+
4+
public SumBetweenRange(int[] nums) {
5+
this.prefixSum = new int[nums.length];
6+
this.prefixSum[0] = nums[0];
7+
for (int i = 1; i < nums.length; i++) {
8+
this.prefixSum[i] = this.prefixSum[i - 1] + nums[i];
9+
}
10+
}
11+
12+
public int sumRange(int i, int j) {
13+
if (i == 0) {
14+
return this.prefixSum[j];
15+
}
16+
return this.prefixSum[j] - this.prefixSum[i - 1];
17+
}
18+
}

0 commit comments

Comments
 (0)