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:

  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.

 
 
 
class NumArray
{
public:
NumArray(vector<int> &nums)
{
sums.push_back();
for (int i = ; i < nums.size(); i++)
{
sums.push_back(sums[i] + nums[i]);
values.push_back(nums[i]);
}
} void update(int i, int val)
{
int diff = val - values[i];
for (int k = i + ; k < sums.size(); k++)
sums[k] = sums[k] + diff;
values[i] = val;
} int sumRange(int i, int j)
{
return sums[j + ] - sums[i];
}
private:
vector<int> sums;
vector<int> values;
};

最新文章

  1. 通过innobackupex实现对MySQL的增量备份与还原
  2. 长轮询(long polling)
  3. 前端工程化开发之yeoman、bower、grunt
  4. Android -- 初探MVP模式
  5. tomcat异常: Cannot get a connection, pool exhausted
  6. bzoj 2241: [SDOI2011]打地鼠
  7. cocos2d-x3.2中怎样优化Cocos2d-X游戏的内存
  8. Installshield停止操作系统进程的代码--IS5版本适用
  9. 老李分享:网页爬虫java实现
  10. Python中serial的使用
  11. C# 中将月份格式化为英语缩写格式
  12. Windows 常用的 CMD 命令
  13. Cannot invoke Tomcat manager: socket write error
  14. mac系统下安装Windows(7,8,10都一样的步骤)
  15. Oracle- 复杂查询及总结
  16. html5 &amp; input &amp; accept attribute
  17. vs2013发布网站合并程序是出错(ILmerge.merge:error)
  18. Centos6安装oracle10g
  19. Monkeyrunner 简介及其环境搭建
  20. (19)zabbix Applications使用介绍

热门文章

  1. oracle 表分区例子
  2. adb占用
  3. mathematica9激活
  4. mysql实现分页的几种方式
  5. 2018.10.20 bzoj1079: [SCOI2008]着色方案(多维dp)
  6. 2018.06.30 BZOJ1026: [SCOI2009]windy数(数位dp)
  7. Django入门与实践-第20章:QuerySets(查询结果集)(完结)
  8. Netty学习第一节Netty的总体概况
  9. JavaNIO学习一
  10. Node.js使用MySQL的连接池