Files
LeetCode-Go/leetcode/0303.Range-Sum-Query-Immutable/303. Range Sum Query - Immutable.go

56 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package leetcode
import (
"github.com/halfrost/LeetCode-Go/template"
)
//解法一 线段树sumRange 时间复杂度 O(1)
// NumArray define
type NumArray struct {
st *template.SegmentTree
}
// Constructor303 define
func Constructor303(nums []int) NumArray {
st := template.SegmentTree{}
st.Init(nums, func(i, j int) int {
return i + j
})
return NumArray{st: &st}
}
// SumRange define
func (ma *NumArray) SumRange(i int, j int) int {
return ma.st.Query(i, j)
}
//解法二 prefixSumsumRange 时间复杂度 O(1)
// // NumArray define
// type NumArray struct {
// prefixSum []int
// }
// // Constructor303 define
// func Constructor303(nums []int) NumArray {
// for i := 1; i < len(nums); i++ {
// nums[i] += nums[i-1]
// }
// return NumArray{prefixSum: nums}
// }
// // SumRange define
// func (this *NumArray) SumRange(i int, j int) int {
// if i > 0 {
// return this.prefixSum[j] - this.prefixSum[i-1]
// }
// return this.prefixSum[j]
// }
/**
* Your NumArray object will be instantiated and called as such:
* obj := Constructor(nums);
* param_1 := obj.SumRange(i,j);
*/