Golang | Leetcode Golang题解之第303题区域和检索-数组不可变

题目:

题解:

Go 复制代码
type NumArray struct {
    sums []int
}

func Constructor(nums []int) NumArray {
    sums := make([]int, len(nums)+1)
    for i, v := range nums {
        sums[i+1] = sums[i] + v
    }
    return NumArray{sums}
}

func (na *NumArray) SumRange(i, j int) int {
    return na.sums[j+1] - na.sums[i]
}
相关推荐
Elias不吃糖10 小时前
LeetCode 71:简化 Unix 路径(Simplify Path)——栈 / vector
算法·leetcode·
sheeta199810 小时前
LeetCode 每日一题笔记 日期:2025.12.15 题目:2110.股票平滑下跌阶段的数目
笔记·算法·leetcode
lightqjx1 天前
【算法】双指针
c++·算法·leetcode·双指针
sin_hielo1 天前
leetcode 2147
数据结构·算法·leetcode
萌>__<新1 天前
力扣打卡每日一题——缺失的第一个正数
数据结构·算法·leetcode
萌>__<新1 天前
力扣打卡每日一题————零钱兑换
算法·leetcode·职场和发展
古城小栈1 天前
Golang 中 return 与 defer 的 长幼尊卑
golang
重生之后端学习1 天前
238. 除自身以外数组的乘积
java·数据结构·算法·leetcode·职场和发展·哈希算法
Learner__Q1 天前
每天五分钟:动态规划-LeetCode高频题_day2
算法·leetcode·动态规划
teamlet1 天前
Gear DNS - 一个go语言开发的小型dns系统
golang·dns·网络服务