【刷题-LeetCode】307. Range Sum Query - Mutable
- Range Sum Query - Mutable
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
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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- 307. Range Sum Query - Mutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- leetcode 307 Range Sum Query
问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ...
- 【leetcode】307. Range Sum Query - Mutable
题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...
随机推荐
- 不同机房vpc使用openswan打通内网
1.测试环境: 北京6云主机:120.92.51.75/10.0.3.13 VPC:10.0.0.0/16 Ren-test 上海2云主机:42.157.163.120/192.168.3.3 ...
- mysql如何查询某个库,某个表都有哪些字段
如下语句便可查看 SELECT column_name FROM Information_schema.columns WHERE table_Name = 'columns' AND TABLE_ ...
- JAVA提取字符串中所有的URL链接,并加上a标签
工具类 Patterns.java 1 package com.util; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.P ...
- Spring实现自定义注解并且配置拦截器进行拦截
有时候我们会自定义注解,并且需要配置拦截器对请求方法含有该自定义注解的方法进行拦截操作 自定义注解类 NeedToken.java import java.lang.annotation.Docume ...
- windows10源码编译llvm
准备 cmake, 我目前使用的版本是3.18 llvm 源码, 我下载的是 11.0 我已经具备Vs2015和Vs2017的开发环境. debug模式编译需要较多内存和较多硬盘存储空间. (debu ...
- 【九度OJ】题目1474:矩阵幂 解题报告
[九度OJ]题目1474:矩阵幂 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1474 题目描述: 给定一个n*n的矩阵,求该矩阵的 ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 一文解析Apache Avro数据
摘要:本文将演示如果序列化生成avro数据,并使用FlinkSQL进行解析. 本文分享自华为云社区<[技术分享]Apache Avro数据的序列化.反序列&&FlinkSQL解析 ...
- KISS原则
Keep It Simple, Stupid 1. 模块性原则:写简单的,通过干净的接口可被连接的部件:2. 清楚原则:清楚要比小聪明好.3. 合并原则:设计能被其它程序连接的程序.4. 分离原则:从 ...
- Linux_Cornd任务调度
Crond任务调度 进行定时任务的设置 概述 任务调度:是指系统在某个时间执行特定的命令或程序 作用:避免重复工作 基本语法 crontab [选项] 选项 功能 -e 编辑crontab定时任务 - ...