Update solution 0307

This commit is contained in:
YDZ
2021-04-22 20:36:55 +08:00
parent 732095c59a
commit 8a14f2c734
4 changed files with 87 additions and 10 deletions

View File

@ -1,8 +1,6 @@
package leetcode
import (
"github.com/halfrost/LeetCode-Go/template"
)
import "github.com/halfrost/LeetCode-Go/template"
// NumArray define
type NumArray struct {
@ -23,6 +21,11 @@ func (this *NumArray) Update(i int, val int) {
this.st.Update(i, val)
}
// SumRange define
func (this *NumArray) SumRange(i int, j int) int {
return this.st.Query(i, j)
}
//解法二 prefixSumsumRange 时间复杂度 O(1)
// // NumArray define
@ -60,6 +63,30 @@ func (this *NumArray) Update(i int, val int) {
// return this.prefixSum[j]
// }
// 解法三 树状数组
// type NumArray struct {
// bit template.BinaryIndexedTree
// data []int
// }
// // Constructor define
// func Constructor307(nums []int) NumArray {
// bit := template.BinaryIndexedTree{}
// bit.InitWithNums(nums)
// return NumArray{bit: bit, data: nums}
// }
// // Update define
// func (this *NumArray) Update(i int, val int) {
// this.bit.Add(i+1, val-this.data[i])
// this.data[i] = val
// }
// // SumRange define
// func (this *NumArray) SumRange(i int, j int) int {
// return this.bit.Query(j+1) - this.bit.Query(i)
// }
/**
* Your NumArray object will be instantiated and called as such:
* obj := Constructor(nums);