-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNumArray.go
More file actions
44 lines (35 loc) · 880 Bytes
/
Copy pathNumArray.go
File metadata and controls
44 lines (35 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* https://leetcode.com/problems/range-sum-query-immutable/#/description
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
Note:
You may assume that the array does not change.
There are many calls to sumRange function.
*/
package ldp
type NumArray struct {
sums []int
}
func Constructor(nums []int) NumArray {
a := NumArray{}
for i := 1; i < len(nums); i++ {
nums[i] = nums[i-1] + nums[i]
}
a.sums = nums
return a
}
func (this *NumArray) SumRange(i int, j int) int {
if i > j || i < 0 || i > len(this.sums)-1 || j > len(this.sums)-1 {
panic("i cannot > j or i index outof range")
}
var m int
if i == 0 {
m = 0
} else {
m = this.sums[i-1]
}
return this.sums[j] - m
}