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. 对QuerySet的理解

    1. 如何通过Django的Model操作数据库? 在Django的Model中,QuerySet是一个很重要的概念.因为我们同数据库的所有查询以及更新交互都是通过它来完成的. 2. Django的M ...

  2. CF935B Fafa and the Gates 题解

    Content 一个动点 \(F\) 一开始在平面直角坐标系上原点的位置,随后它会移动 \(n\) 次,每次只能向上走或者向右走 \(1\) 个单位,求经过直线 \(y=x\) 的次数. 数据范围:\ ...

  3. java 多线程:Thread类;Runnable接口

    1,进程和线程的基本概念: 1.什么是进程: 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机 ...

  4. libevent源码学习(10):min_heap数据结构解析

    min_heap类型定义min_heap函数构造/析构函数及初始化判断event是否在堆顶判断两个event之间超时结构体的大小关系判断堆是否为空及堆大小返回堆顶event分配堆空间堆元素的上浮堆元素 ...

  5. JAVA删除某个文件夹(递归删除文件夹的所有文件)

    /** * 递归删除文件夹下所有内容 最后删除该文件夹 * @param filePath 要删除的文件夹路径 * @return */ public boolean deleteFiles(Stri ...

  6. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  7. 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...

  8. Codeforces 888D: Almost Identity Permutations(错排公式,组合数)

    A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...

  9. SuperPixel

    目录 SLIC Superpixel algorithm 距离函数的选择 代码 Gonzalez R. C. and Woods R. E. Digital Image Processing (For ...

  10. Proximal Algorithms 2 Properties

    目录 可分和 基本的运算 不动点 fixed points Moreau decomposition 可分和 如果\(f\)可分为俩个变量:\(f(x, y)=\varphi(x) + \psi(y) ...