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.
vector<int> num,num[i]意义是nums的第0项到第i项的和,计算range(i,j)时只需要计算num[j]-num[i-1],要考虑边界情况,并且在更新数组中某个位置处的值后,应该更新对应的num
Code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;