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
// @Title: 区域和检索 - 数组不可变 (Range Sum Query - Immutable)
// @Author: 15816537946@163.com
// @Date: 2019-11-20 16:10:38
// @Runtime: 36 ms
// @Memory: 8.4 MB
type NumArray struct {
dp []int    
}


func Constructor(nums []int) NumArray {
	size := len(nums)
	dp := make([]int, size+1)
	for i :=1;i<=size;i++ {
		dp[i] = dp[i-1] + nums[i-1]
	}

	return NumArray{dp}
}


func (this *NumArray) SumRange(i int, j int) int {
	return this.dp[j+1] - this.dp[i]
}


/**
 * Your NumArray object will be instantiated and called as such:
 * obj := Constructor(nums);
 * param_1 := obj.SumRange(i,j);
 */