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:12345Given nums = [1, 3, 5]sumRange(0, 2) -> 9update(1, 2)sumRange(0, 2) -> 8
Note:
The array is only modifiable by the update function.
You may assume the number of calls to update and sumRange function is distributed evenly.
解法1: Segment Tree
Segment Tree的直接应用。
Segment Tree 的node需要记录range的start,end以及对应的sum。
tree需要有一个node作为root。tree提供如下几个基本的method:1build,update,sumRange
每一个函数基本都是通过recursion实现的。
|
|