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), 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
Analysis:
一开始想着用一个简单的数组存取并进行更新和求和就可以,实际写的时候发现不太可行,网上找了找发现了一种新的数据结构或者说一种新的方法——树状数组(Binary Index Tree ,or Fenwick Tree)。
树状数组常用于修改某点的值、求某个区间的和。普通数组修改某点值时间复杂度是O(1),查询的时间复杂度是O(n),对于树状数组来说修改和查询的时间复杂度都是O(logn)。
Reference——http://blog.csdn.net/int64ago/article/details/7429868


树状数组是巧妙地利用了二分,先介绍两个个“工具”——lowbit(k)和“加尾”操作。
lowbit(k)就是把k的二进制高位全部清0,只留下最低位1,如lowbit(0101)=0001,实现:lowbit(k)=k&(-k)。
加尾操作,把一个数k加上它自己的lowbit(k),k=k+lowbit(k),如去尾(0011)=0011+lowbit(0011)=0100。
对a数组进行“操作”实际上是对c数组进行操作,也就是说我们正真用到的是c数组,那么c和a之间有某种关系,首先我们要找到这种关系,这就要用到第一个工具lowbit(k)。c[k]=a[k]向左边求lowbit(k)个和,比如c[0110]=a[0110]向左边求lowbit(0110)个和,也就是向左求两个和,则c[0110]=a[0110]+a[0101]。由这种方法就可以确定ck是由a中哪几个元素确定,如上图。
上面说的是如何由c找到对应的a,那么由a找到对应的c就要用到加尾操作,因为如果我们要对a中某个值进行更改,c中的值肯定也要对应更改,所以要找到a对应的所有c。如a[0011]对应的c,加尾(0011)=0011+lowbit(0011)=0100,加尾(0100)=0100+lowbit(0100)=1000,所以a[0011]对应3个c,c[0011],c[0100],c[1000]。
由右边的图我们可以更直观地看出对应关系,如c4直接包含了c2,c3(a3),c4(a4)(直接包括的黑方块),而c2直接包含了a1,a2,那么c4就包含了a1~a4,再如c6,包含了c6(a6),c5(a5)。
a和c的对应关系就是这样了,为了对应更加方便,通常用的时候声明数组会多给出一个空间,从下标为1开始用,下标为0的不用,下面给出的方法也是从1开始。
//对a数组中的某一值进行加操作
void add(int k,int num)
{
while(k<=n)
{
tree[k]+=num;
k+=k&-k;
}
}
//对某一值进行更改时
void change(int k,int num)
{
int n=num-tree[k];//区别于对某值进行加操作,加操作是在原有基础上更改,而如果要直接对某值更新时要借用加操作
while(k<=n) //那么下面这段while可以改成add(n,num);
{
tree[k]+=num;
k+=k&-k;
}
}
int read(int k)//求1~k的区间和
{
int sum=0;
while(k>0)
{
sum+=tree[k];
k-=k&-k; //求区间和时会反向用加尾操作,即把c对应的a全部找出来
}
return sum;
}
int sumIK(int i,int k)//i~k的区间和
{
sum(i);
sum(k+1);
return sum(k+1)-sum(i);//借用前面的求和,但是要注意,实际上应该是k-(i-1),因为从1开始,所以两边都要加上1
}
Solution:
public class NumArray {
int[] btree;
int[] arr;
public NumArray(int[] nums) {
btree = new int[nums.length+1];
arr = nums;
for(int i=0; i<nums.length; i++){
add(i+1, nums[i]);
}
}
void add(int i,int value){
for(;i<btree.length;i+=i&(-i))
{
btree[i]+=value;
}
}
void update(int i, int val) {
add(i+1, val-arr[i]);
arr[i]=val;
}
public int sumRange(int i, int j) {
return sumHelp(j+1)-sumHelp(i);
}
public int sumHelp(int i){
int sum=0;
for(;i>=1;i-=i&(-i))
{
sum+=btree[i];
}
// while((boolean)i)
// {
// sum+=btree[i];
// i-=i&(-i);
// }
return sum;
}
}
// Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.update(1, 10);
// numArray.sumRange(1, 2);
Leetcode 2——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 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 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(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
随机推荐
- 芝麻HTTP:爬虫之设置Selenium+Chrome代理
微博登录限制了错误次数···加上Cookie大批账号被封需要从Cookie池中 剔除被封的账号··· 需要使用代理··· 无赖百度了大半天都是特么的啥玩意儿???结果换成了 Google手到擒来 分分 ...
- dependencies 与 devDependencies 的区别
dependencies 与 devDependencies 的区别 在使用 npm install 安装 npm 包时,有两种命令参数可以把它们的信息写入 package.json 文件: --sa ...
- jQuery 中 jQuery(function(){})与(function(){})(jQuery) 的区别及用法
query是优秀的Javascrīpt框架.我们现在来讨论下在 Jquery 中两个页面载入后执行的函数. $(document).ready(function(){ // 在这里写你的代码... } ...
- [前端]如何写一个水平导航栏?(浮动、inline-block+消除间距)
在看W3school时,看到一个很好的例子,如何制作一个水平的导航栏?没有任何要求,只需要达到下面的效果: 我认为这个例子包含了很多css布局需要了解的知识,因此单独写一下. W3school上面的方 ...
- Java并发编程-各种锁
安全性和活跃度通常相互牵制.我们使用锁来保证线程安全,但是滥用锁可能引起锁顺序死锁.类似地,我们使用线程池和信号量来约束资源的使用, 但是缺不能知晓哪些管辖范围内的活动可能形成的资源死锁.Java应用 ...
- [BZOJ1012] [JSOI2008] 最大数maxnumber (ST表)
Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插 ...
- Delphi关于TAdvStringGrid控件颜色的设置
FixedFont-Color 标题行和列字体的颜色 Font-color 单元格字体的颜色Visual-color 整个表格的背景色填充
- 24时区,GMT,UTC,DST,CST时间详解
全球24个时区的划分 相较于两地时间表,可以显示世界各时区时间和地名的世界时区表(World Time),就显得精密与复杂多了,通常世界时区表的表盘上会标示着全球24个时区的城市名称,但究竟 ...
- 关于如何使用SVN的一些建议
SVN是管理源码的主流方式之一,当多人同时编辑同一项目时经常会出现冲突,本文主要针对Asp.net 项目开发中使用SVN提出一点建议. 1.忽略asp.net 项目中的非源代码文件 .VS目录是vis ...
- 关于java构造函数,静态代码块,构造代码块,和普通代码块相关总结(一)
构造函数.构造代码块和静态代码块容易混淆,它们的执行条件和执行顺序也常常容易犯迷.这里就针对这些问题说一下我个人的一些理解,顺便对这部分内容做个小结. 一.构造函数 格式:类名(参数1,参数2,-){ ...