leetcode笔记: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
Note:
The array is only modifiable by the update function.
You may assume the number of calls to update and sumRange function is distributed evenly.
二. 题目分析
题目在Range Sum Query - Immutable一题的基础上添加的难度,要求在输入数组nums后,可以改动数组的元素,每次仅仅改动一个元素。相同要实现求数组的某个区间和的功能。
假设在http://blog.csdn.net/liyuefeilong/article/details/50551662 一题的做法上稍加改进。是可以实现功能的,可是会超时。
这时阅读了一些文章后才发现原来经典的做法(包含前一题)是使用树状数组来维护这个数组nums。其插入和查询都能做到O(logn)的复杂度,十分巧妙。
关于树状数组,下面博文描写叙述得非常好:
http://blog.csdn.net/int64ago/article/details/7429868
三. 演示样例代码
// 超时
class NumArray {
public:
NumArray(vector<int> &nums) {
if (nums.empty()) return;
else
{
sums.push_back(nums[0]);
//求得给定数列长度
int len = nums.size();
for (int i = 1; i < len; ++i)
sums.push_back(sums[i - 1] + nums[i]);
}
}
void update(int i, int val) {
for (int k = i; k < nums.size(); ++k)
sums[k] += (val - nums[i]);
nums[i] = val;
}
int sumRange(int i, int j) {
return sums[j] - sums[i - 1];
}
private:
vector<int> nums;
//存储数列和
vector<int> sums;
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.update(1, 10);
// numArray.sumRange(1, 2);
// 使用树状数组实现的代码AC,复杂度为O(logn)
class NumArray {
private:
vector<int> c;
vector<int> m_nums;
public:
NumArray(vector<int> &nums) {
c.resize(nums.size() + 1);
m_nums = nums;
for (int i = 0; i < nums.size(); i++){
add(i + 1, nums[i]);
}
}
int lowbit(int pos){
return pos&(-pos);
}
void add(int pos, int value){
while (pos < c.size()){
c[pos] += value;
pos += lowbit(pos);
}
}
int sum(int pos){
int res = 0;
while (pos > 0){
res += c[pos];
pos -= lowbit(pos);
}
return res;
}
void update(int i, int val) {
int ori = m_nums[i];
int delta = val - ori;
m_nums[i] = val;
add(i + 1, delta);
}
int sumRange(int i, int j) {
return sum(j + 1) - sum(i);
}
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.update(1, 10);
// numArray.sumRange(1, 2);
四. 小结
之前仅仅是听过,这是第一次使用树状数组,这是维护数组的一个非常好的思想。须要深入学习!
leetcode笔记:Range Sum Query - Mutable的更多相关文章
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- [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 2——Range Sum Query - Mutable(树状数组实现)
Problem: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- 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 308. Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
随机推荐
- python面向对象的成员、属性等
#类成员: #字段 self.xy =qq . xy=qq #普通字段 (保存在对象里面) #直接通过点(.)+ 字段 进行调用 #静态字段 (保存在类里面) #静态字段属于类,在内存只保留一份 . ...
- double int 类型的区别
内部组织格式不同: po [NSString stringWithFormat:@"%d", f] 107886912 (lldb) po [NSString stringWith ...
- [USACO10NOV]奶牛的图片Cow Photographs 树状数组 递推
Code: #include<cstdio> #include<algorithm> #include<string> #include<cstring> ...
- (noip模拟十七)【BZOJ3930】[CQOI2015]选数-容斥水法
Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公 ...
- 【PRML学习笔记】第四章:分类的线性模型
一.基础概念 线性分类模型:决策面(decision boundary)是输入向量的线性函数 目标类别的表示"1 of K" :$ t = (0,1,0,0,0)^T$ 二.分类问 ...
- 紫书 习题 8-24 UVa 10366 (构造法)
又是一道非常复杂的构造法-- #include<cstdio> #include<algorithm> #define REP(i, a, b) for(int i = (a) ...
- John Morgan:黎曼几何、曲率、Ricci流以及在三维流形上的应用二讲
本文是笔者在线看Lektorium上John Morgan在圣彼得堡国立大学欧拉研究所的讲座做的笔记.第一讲以如下内容组成 1. 黎曼曲面上的联络 黎曼流形$(M^n,g)$中,$M$为$n$维流形, ...
- unity3D常见问题
总结自己在学习中遇到的问题. 记录问题,帮助他人,有什么不正确的地方欢迎指正 没有发生碰撞 两个物体(Plane和Cube)都加入了collider,当中一个加入了rigidbody,应该会产生碰撞, ...
- Hive中建表注释为乱码的解决方式
Hive中建表注释为乱码的解决方式 可以查看http://www.cnblogs.com/stono/p/7813711.html进行手动修改: 如果要解决,在Ambari配置界面中,选择Hive,输 ...
- UItableView中的一些方法
有关UITableView的知识点相对照较简单,一些简单的经常使用的方法有时间在写上: 以下的几个方法假设仅仅是展示UITableView是用不到的,须要对对应的区段进行操作的时候才会用到. 方法例如 ...