首页 > 其他分享 >LeetCode: 307. Range Sum Query - Mutable

LeetCode: 307. Range Sum Query - Mutable

时间:2022-12-05 18:00:13浏览次数:55  
标签:end nums int Sum tree beg len 307 Range


LeetCode: 307. Range Sum Query - Mutable

题目描述

Given an integer array nums, find the sum of the elements between indices ​​i​​​ and ​​j (i ≤ j)​​, inclusive.

The ​​update(i, val)​​​ function modifies nums by updating the element at index ​​i​​​ to ​​val​​.

Example:

Given nums = [1, 3, 5]

sumRange(0, 2) -> 9
update(1, 2)
sumRange(0, 2) -> 8

Note:

  1. The array is only modifiable by the ​​update​​ function.
  2. You may assume the number of calls to ​​update​​​ and ​​sumRange​​ function is distributed evenly.

解题思路 —— 线段树(segment tree)

  1. 将给定数组扩展成满二叉树的叶子,不足的部分用 ​​0​​ 补足
  2. 每次更新数组时,循环更新它的祖先节点
  3. 每次查询区间时,从根节点递归向下寻找满足在指定区间内的节点

AC 代码

type NumArray struct {
tree []int
}

func Constructor(nums []int) NumArray {
var arr NumArray

if len(nums) == 0 { return arr }

// 形成满二叉树
for len(nums)&(len(nums)-1) != 0 {
nums = append(nums, 0)
}

arr.tree = make([]int, len(nums)*2)
arr.tree[0] = -1

// 生成线段树
for i := len(nums); i >= 1; i /= 2 {
for j := i; j < i*2; j++{
if j >= len(nums) {
arr.tree[j] = nums[j-len(nums)]
} else {
arr.tree[j] = arr.tree[j*2] + arr.tree[j*2+1]
}
}
}

return arr
}


func (this *NumArray) Update(i int, val int) {
// 更新线段树
for j := len(this.tree)/2 + i; j >= 1; j /= 2 {
if j == len(this.tree)/2 + i {
this.tree[j] = val
} else {
this.tree[j] = this.tree[j*2] + this.tree[j*2+1];
}
}

}

func min(m, n int) int {
if m > n {
return n
} else {
return m
}
}

func max(m, n int) int {
return (m + n) - min(m, n)
}

func (this *NumArray) SumRangeRef(i int, j int, idx int, beg int, end int) int {

// 区间 [i, j] 和 区间 [beg, end] 不相交
if beg > j || end < i || end < beg {
return 0
}


// 区间 [beg, end] 包含于区间 [i, j]
if beg >= i && end <= j {
return this.tree[idx]
}

ans := 0
mid := (beg + end) / 2

// 区间 [beg, mid] 和 区间 [i, j] 的交集
l1 := max(beg, i)
r1 := min(j, mid)
if l1 <= r1 {
ans += this.SumRangeRef(l1, r1, idx*2, beg, mid)
}

// 区间 [mid+1, end] 和 区间 [i, j] 的交集
l2 := max(mid+1, i)
r2 := min(j, end)
if l2 <= r2 {
ans += this.SumRangeRef(l2, r2, idx*2+1, mid+1, end)
}

return ans
}

func (this *NumArray) SumRange(i int, j int) int {
return this.SumRangeRef(i+1, j+1, 1, 1, len(this.tree)/2)
}


/**
* Your NumArray object will be instantiated and called as such:
* obj := Constructor(nums);
* obj.Update(i,val);
* param_2 := obj.SumRange(i,j);
*/


标签:end,nums,int,Sum,tree,beg,len,307,Range
From: https://blog.51cto.com/u_15903085/5913226

相关文章

  • HTML5中新的summary和details标签学习
    HTML5中的标签是越来越多了,下面挖掘两个居然在标准中提到的,但依然绝大部分浏览器都不支持的标签,目前只有chrome支持(最新版本)。所谓的summary和detai......
  • [Typescript] 126. Hard - Two Sum
    Givenanarrayofintegers nums andaninteger target,returntrueiftwonumberssuchthattheyaddupto target./*_____________YourCodeHere________......
  • Typescript类型题材 - NumberRange
    题目中文有时我们需要限制数字的范围...示例:typeresult=NumberRange<2,9>//|2|3|4|5|6|7|8|9EnglishSometimeswewanttolimittheran......
  • two number sum
     nestedloopremand=[3,5,-4,8,11,1,-1,6]goal=10defpalindrome(array:list[int],goal:int)->tuple:length=len(array)foriinra......
  • 2022-2023-1 20221307《计算机基础和程序设计》第十四周学习总结
    这个作业属于那个班级 https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业要求 https://www.cnblogs.com/rocedu/p/9577842.html#WEEK14作业目标学习《C语言程......
  • SumatraPDF-3.2-64下载,开发必备,非常小巧的PDF阅读器
    关注微信公众号【工控羊】或者微信号【gksheep】,微信公众号后台输入数字编号【2020】即可获取下载链接。......
  • Windbg提示:*** WARNING: Unable to verify checksum for 的处理
    当我们用windbg调试时,经常会遇到“***WARNING:Unabletoverifychecksumforxxx.dll”这样的提示,他的意思时不能校验某某模块的校验和。这一般都是我们的动态库或exe的......
  • lwl-resume 个人简历编辑使用说明
    LWLRESUME一个简洁、可自定义生成pdf的在线简历编辑工具!前言我们一般都是使用word编写简历,但是排版问题比较头疼,非常耗时不说,最后转换pdf有些地方可能还会样式错乱……......
  • Nsum问题
    双指针解决NSum问题1.两数之和publicint[]twoSum(int[]nums,inttarget){/***首先,对于该题要求返回原始数组下标,这里就要求不能手动排序......
  • CodeForces - 476C-Dreamoon and Sums(数学思维)
    C.DreamoonandSums题解:设则有题目所求可得所以代码#include<bits/stdc++.h>typedeflonglongLL;usingnamespacestd;constintmod=1e9+7;intmain(){#ifndefO......