Skip to content

Commit fc6ce50

Browse files
authored
Create 0303-range-sum-query-immutable.kt
1 parent 78f714d commit fc6ce50

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: kotlin/0303-range-sum-query-immutable.kt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class NumArray(nums: IntArray) {
2+
3+
val prefix = IntArray(nums.size)
4+
5+
init {
6+
var sum = 0
7+
for((i,v) in nums.withIndex()) {
8+
sum += v
9+
prefix[i] = sum
10+
}
11+
}
12+
13+
fun sumRange(left: Int, right: Int): Int {
14+
val prefixR = prefix[right]
15+
val prefixL = if(left == 0) 0 else prefix[left - 1]
16+
return prefixR - prefixL
17+
}
18+
19+
}
20+
21+
/**
22+
* Your NumArray object will be instantiated and called as such:
23+
* var obj = NumArray(nums)
24+
* var param_1 = obj.sumRange(left,right)
25+
*/

0 commit comments

Comments
 (0)