0%

LeetCode-303

题目

结果

代码

直接进行一个动态的规划

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
type NumArray struct {
sums []int
}

func Constructor(nums []int) NumArray {
sums := make([]int, len(nums)+1)
for i, v := range nums {
sums[i+1] += sums[i] + v
}
return NumArray{sums}
}

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

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