(14)Go实现的线段树解答leetcode-307

(14)Go实现的线段树解答leetcode-307_第1张图片
// 这类问题用线段树解决是比较好的选择
   解决方法的定义:

type NumArray struct {
    tree   []int
    data   []int
}


func Constructor(nums []int) NumArray {
    length := len(nums)

    if length==0{
        return NumArray{}
    }
    tree := &NumArray{
        tree:   make([]int, length*4),
        data:   nums,
    }

    tree.bulidSegmentTree(0, 0, length-1)

    return *tree
}

func (this *NumArray) bulidSegmentTree(index, l, r int) int {
    if l == r {
        this.tree[index] = this.data[l]
        return this.data[l]
    }

    leftI := 2*index+1
    rightI := leftI + 1
    mid := l + (r-l)/2
    leftResp := this.bulidSegmentTree(leftI, l, mid)
    rightResp := this.bulidSegmentTree(rightI, mid+1, r)

    this.tree[index] = leftResp + rightResp
    return this.tree[index]
}


func (this *NumArray) Update(i int, val int)  {
    length := len(this.data)
    if i < 0 || i >= length {
        return
    }
    this.set(0, 0, length-1, i, val)
}

func (this *NumArray) set(treeIndex, l, r, k, v int) {
    if l == r {
        this.tree[treeIndex] = v
        return
    }

    leftI := 2*treeIndex+1
    rightI := leftI + 1
    midI := l + (r-l)/2

    if k > midI {
        this.set(rightI, midI+1, r, k, v)
    } else {
        this.set(leftI, l, midI, k, v)
    }

    this.tree[treeIndex] = this.tree[leftI]+this.tree[rightI]
}

func (this *NumArray) SumRange(i int, j int) int {
    length := len(this.data)
    if i < 0 || i > j || j >= length {
        return 0
    }
    return this.queryrange(0, 0, length-1, i, j)
}

func (this *NumArray) queryrange(index, i1, i2, queryL, queryR int) int {
    if i1 == queryL && i2 == queryR {
        return this.tree[index]
    }

    leftI := 2*index+1
    rightI := leftI + 1
    mid := i1 + (i2-i1)/2

    if queryL > mid {
        return this.queryrange(rightI, mid+1, i2, queryL, queryR)
    }
    if queryR <= mid {
        return this.queryrange(leftI, i1, mid, queryL, queryR)
    }
    leftResp := this.queryrange(leftI, i1, mid, queryL, mid)
    rightResp := this.queryrange(rightI, mid+1, i2, mid+1, queryR)
    return leftResp + rightResp
}
用leetcode测试代码逻辑
(14)Go实现的线段树解答leetcode-307_第2张图片
通过,逻辑通。

有bug欢迎指出,转载请注明出处。

你可能感兴趣的:((14)Go实现的线段树解答leetcode-307)