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 线段树(不想看了。。。)

【刷题-LeetCode】307. Range Sum Query - Mutable的更多相关文章

  1. [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  2. leetcode@ [307] Range Sum Query - Mutable / 线段树模板

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  3. [LeetCode] 307. Range Sum Query - Mutable 解题思路

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  4. LeetCode - 307. Range Sum Query - Mutable

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  5. leetcode 307. Range Sum Query - Mutable(树状数组)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  6. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  7. 307. Range Sum Query - Mutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  8. leetcode 307 Range Sum Query

    问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ...

  9. 【leetcode】307. Range Sum Query - Mutable

    题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...

随机推荐

  1. LuoguP2556 [AHOI2002]黑白图像压缩 题解

    Content 题目描述太过于繁琐而无法简化,请前往原题面查看. 数据范围:\(1\leqslant n\leqslant 8\times 10^4\). Solution & Code 一个 ...

  2. LuoguP7859 [COCI2015-2016#2] GEPPETTO 题解

    Content 有 \(n\) 个数 \(1\sim n\).你需要在其中选若干个数.但是还有 \(m\) 个限制,第 \(i\) 个限制格式为 \(a_i\) 不能和 \(b_i\) 一起选.问你一 ...

  3. 【C语言】Socket发送HTTP-TCP请求,数据有字符串插入

    问题描述: 场景:编写Socket接口,向LOKI发送POST请求查询数据 BUG发现位置:通过cJSON读取时间戳,发现被截断. 现象:通过read()去读取返回的数据,数据行中被插入字符:如下 c ...

  4. git提交报错:Error merging: refusing to merge unrelated histories

    执行: git pull origin master --allow-unrelated-histories 然后再重新push即可

  5. JAVA结合 JSON Web Token(JWT) 工具类

    引入java-jwt-3.3.0.jar .  jjwt-0.9.0.jar .jackson-all-1.7.6.jar 或者maven <!-- https://mvnrepository. ...

  6. JAVA获取本机的MAC地址

    /** * 获取本机的Mac地址 * @return */ public String getMac() { InetAddress ia; byte[] mac = null; try { // 获 ...

  7. C++基础之虚析构函数原理

    结论 虚函数表指针 + 虚函数表 共同实现 演示 VS2017(32位) 基类有析虚构函数 一段代码演示 #include <iostream> #include <memory&g ...

  8. 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...

  9. 【九度OJ】题目1064:反序数 解题报告

    [九度OJ]题目1064:反序数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1064 题目描述: 设N是一个四位数,它的 ...

  10. World is Exploding(hdu5792)

    World is Exploding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...