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:
- The array is only modifiable by the update function.
- You may assume the number of calls to update and sumRange function is distributed evenly.
class NumArray { List<Integer> list = new ArrayList(); public NumArray(int[] nums) { for(int i: nums) list.add(i); } public void update(int i, int val) { list.set(i, val); } public int sumRange(int i, int j) { int res = 0; for(int m = i; m <= j; m++) res+=list.get(m); return res; } }
No segment tree today.
标签:val,nums,int,update,Range,Query,sumRange,Mutable,public 来源: https://www.cnblogs.com/wentiliangkaihua/p/12855397.html