mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +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);
|
||||
|
@ -12,9 +12,25 @@ func Test_Problem307(t *testing.T) {
|
||||
obj.Update(1, 2)
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 2))
|
||||
}
|
||||
|
||||
// SumRange define
|
||||
func (ma *NumArray) SumRange(i int, j int) int {
|
||||
return ma.st.Query(i, j)
|
||||
obj = Constructor307([]int{-1})
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 0))
|
||||
obj.Update(0, 1)
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 0))
|
||||
|
||||
obj = Constructor307([]int{7, 2, 7, 2, 0})
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
obj.Update(4, 6)
|
||||
obj.Update(0, 2)
|
||||
obj.Update(0, 9)
|
||||
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(4, 4))
|
||||
obj.Update(3, 8)
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 4))
|
||||
obj.Update(4, 1)
|
||||
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 3))
|
||||
fmt.Printf("SumRange(0,2) = %v\n", obj.SumRange(0, 4))
|
||||
obj.Update(0, 4)
|
||||
}
|
||||
|
@ -48,3 +48,4 @@ sumRange(0, 2) -> 8
|
||||
- 给出一个数组,数组里面的数都是`**可变**`的,设计一个数据结构能够满足查询数组任意区间内元素的和。
|
||||
- 对比第 303 题,这一题由于数组里面的元素都是**`可变`**的,所以第一个想到的解法就是线段树,构建一颗线段树,父结点内存的是两个子结点的和,初始化建树的时间复杂度是 O(log n),查询区间元素和的时间复杂度是 O(log n),更新元素值的时间复杂度是 O(log n)。
|
||||
- 如果此题还用 prefixSum 的思路解答呢?那每次 update 操作的时间复杂度都是 O(n),因为每次更改一个值,最坏情况就是所有的 prefixSum 都要更新一次。prefixSum 的方法在这道题上面也可以 AC,只不过时间排名在 5%,非常差。
|
||||
- 此题也可以用树状数组解决。代码很直白,区间查询即是两个区间前缀和相减。最简单的树状数组应用。
|
||||
|
@ -48,6 +48,7 @@ sumRange(0, 2) -> 8
|
||||
- 给出一个数组,数组里面的数都是`**可变**`的,设计一个数据结构能够满足查询数组任意区间内元素的和。
|
||||
- 对比第 303 题,这一题由于数组里面的元素都是**`可变`**的,所以第一个想到的解法就是线段树,构建一颗线段树,父结点内存的是两个子结点的和,初始化建树的时间复杂度是 O(log n),查询区间元素和的时间复杂度是 O(log n),更新元素值的时间复杂度是 O(log n)。
|
||||
- 如果此题还用 prefixSum 的思路解答呢?那每次 update 操作的时间复杂度都是 O(n),因为每次更改一个值,最坏情况就是所有的 prefixSum 都要更新一次。prefixSum 的方法在这道题上面也可以 AC,只不过时间排名在 5%,非常差。
|
||||
- 此题也可以用树状数组解决。代码很直白,区间查询即是两个区间前缀和相减。最简单的树状数组应用。
|
||||
|
||||
|
||||
## 代码
|
||||
@ -56,9 +57,12 @@ sumRange(0, 2) -> 8
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/template"
|
||||
)
|
||||
import "github.com/halfrost/LeetCode-Go/template"
|
||||
|
||||
// NumArray define
|
||||
type NumArray struct {
|
||||
st *template.SegmentTree
|
||||
}
|
||||
|
||||
// Constructor307 define
|
||||
func Constructor307(nums []int) NumArray {
|
||||
@ -74,6 +78,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
|
||||
@ -111,6 +120,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