mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-24 02:14:00 +08:00
Update solution 0307
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
//解法二 prefixSum,sumRange 时间复杂度 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);
|
||||
|
Reference in New Issue
Block a user