leetcode Range Sum Query – Mutable

leetcode Range Sum Query – Mutable

Given an integer array nums, find the sum of the elements between indices i and j (ij), 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.

题目地址 : leetcode Range Sum Query – Mutable

题意:

给定一个数组,让你正确实现如下两种操作:

  • update(i , val ):让下标为i增加val
  • sumRange(i,j): 计算数组从i到j的值

 

思路:

其实在 leetcode Range Sum Query – Immutable 我已经给多可变数组的解法。

直接上fenwick tree或者线段树都行。

fenwick tree比较好写,水水的1A~

关于fenwick tree看这里:  Fenwick tree (binary indexed tree) 

 

Code

 

 

本博客若无特殊说明则由 hrwhisper 原创发布
转载请点名出处:细语呢喃 > leetcode Range Sum Query – Mutable
本文地址:https://www.hrwhisper.me/leetcode-range-sum-query-mutable/

打赏一杯咖啡钱呗

Leetcode , , , . permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *