1. 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

Constraints:

  • The array is only modifiable by the update function.
  • You may assume the number of calls to update and sumRange function is distributed evenly.
  • 0 <= i <= j <= nums.length - 1

解法1 将查询区间的数字直接求和

class NumArray {
public:
vector<int>Array;
NumArray(vector<int>& nums) {
Array = nums;
} void update(int i, int val) {
Array[i] = val;
} int sumRange(int i, int j) {
int res = 0;
for(int k = i; k <= j; ++k)res += Array[k];
return res;
}
};

解法2 求和数组。先将数组的前n项和计算出来,更新的时候将前k项和(k>= i)更新即可

class NumArray {
public:
vector<int>S{0};
vector<int>Array;
NumArray(vector<int>& nums) {
Array = nums;
for(int i = 0; i < nums.size(); ++i){
S.push_back(S.back() + nums[i]);
}
} void update(int i, int val) {
int d = val - Array[i];
Array[i] = val;
for(int j = i + 1; j < S.size(); ++j)S[j] += d;
} int sumRange(int i, int j) {
return S[j+1] - S[i];
}
};

解法3 分块求和。解法2中update函数花费时间较多,更新的平均时间复杂度为\(O(n/2)\),为了控制更新的范围,将数组划分为多个块,更新控制在对应的块内,将块的尺寸取为\(\sqrt{n}\),更新的时间复杂度为\(O(\sqrt{n})\)

class NumArray {
public:
int block_size;
vector<int>Array;
vector<int>S;
NumArray(vector<int>& nums) {
Array = nums;
block_size = int(sqrt(nums.size()));
int sum = 0;
for(int i = 0; i < nums.size(); ++i){
sum += nums[i];
if((i+1) % block_size == 0 || i + 1 == nums.size()){
S.push_back(sum);
sum = 0;
}
}
} void update(int i, int val) {
S[i / block_size] += val - Array[i];
Array[i] = val;
} int sumRange(int i, int j) {
int res = 0;
int s_b = i / block_size, e_b = j / block_size;
if(s_b == e_b){
for(int k = i; k <= j; ++k)res += Array[k];
}
else{
for(int k = i; k < (s_b+1)*block_size; ++k)res += Array[k];
for(int b =s_b + 1; b < e_b; ++b)res += S[b];
for(int k = e_b*block_size; k <= j; ++k)res += Array[k];
}
return res;
}
};

解法4 线段树(不想看了。。。)

最新文章

  1. jquery轮播图详解,40行代码即可简单解决。
  2. Swift3.0P1 语法指南——函数
  3. discuz教程:discuz模板js与jQuery冲突的解决方案
  4. Spring与jsp表达式的产生的问题
  5. C语言的一点操作(学习笔记)
  6. 写给自己的Java程序员学习路线图
  7. iOS UICollectionView基础
  8. struts2的工作机制
  9. 读书笔记 effective c++ Item 11 在operator=中处理自我赋值
  10. VueJS实现一个货币结算自定义控件
  11. hibernate解读之session--基于最新稳定版5.2.12
  12. PM过程能力成熟度4级
  13. git -分支管理(创建、推送、删除)
  14. “百度杯”CTF比赛 2017 二月场 爆破-3
  15. vue前端面试题知识点整理
  16. 如何安装或卸载Lodop、C-Lodop
  17. 前端学习 -- Html&amp;Css -- 框架集
  18. [No0000131]WCF压缩传输方案整理
  19. CentOS6.5下搭建Samba服务实现与Windows系统之间共享文件资源
  20. API网关之Kong网关简介

热门文章

  1. 第二周Python笔记之 变量的三元运算
  2. python获取命令行传参的两种种常用方法argparse解析getopt 模块解析
  3. Qt5绘制仪表盘dashboard
  4. 【LeetCode】代码模板,刷题必会
  5. 【LeetCode】1079. Letter Tile Possibilities 解题报告 (C++)
  6. 【LeetCode】148. Sort List 解题报告(Python & C++)
  7. hdu-5569matrix(dp)
  8. kafka2.x常用命令笔记(一)创建topic,查看topic列表、分区、副本详情,删除topic,测试topic发送与消费
  9. Electron 使用 Tray设置图标的路径问题
  10. 编写Java程序,定义士兵类(Soldiers)并初始化5个士兵对象。