(LeetCode 307) Range Sum Query - Mutable(树状数组讲解)
来源:互联网 发布:傅园慧表情包爆红网络 编辑:程序博客网 时间:2024/04/25 12:46
Q:
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.
题意大致是,给你提供一个二维数组,然后求指定下标之间的数之和,已知数组中的值可以更新,并且更新和求和操作会被频繁调用
Solution;
最原始的想法就是暴力遍历求和,不过想也不用想,当求和操作操作频繁调用,会超出给定时限。
并且,数组中的值会被频繁更新,我了解到的解决方法有Segement Tree(线段树),Binary Indexed Tree(树状数组) ,和平方根分解三种办法。树状数组真的是神作,很精巧的办法,也很高兴能够了解并学习这些数据结构。
想了解这道题Segement Tree(线段树)解法的可以参考这篇博客:
(LeetCode 307) Range Sum Query - Mutable(Segment Tree)
本篇博客提供树状数组的解释以及解法:
树状数组对于求元素值可变数组指定下标之间值之和的问题十分适合,当然也可应用在求最大值或者最小值这类的问题中。
如果不需要了解树状数组,可以直接翻到最后,会有AC源码。
要了解树状数组,我们先看它的存储形式:
图片来源:树状数组
我们可以看到当
举个例子:
- 当
i=4 时,i=22∗1 ,所以C[4]=A[1]+A[2]+A[3]+A[4] - 当
i=6 时,i=21∗3 ,所以C[6]=A[5]+A[6] - 当
i=8 时,i=23∗1 ,所以C[8]=A[1]+...+A[8] - 当
i=9 时,i=20∗3 ,所以C[6]=A[9]
不知道大家看懂了没有,其实对于奇数偶数都是这么算的。将
那么那个因式怎么得到呢?
有一个简单的公式:
注意,树状数组的下标从1开始。
所以,树状数组中的几个核心函数分别是lowbit,sum, change。这几个函数短小精悍但是力大无穷啊,如果大家能够看懂这几段代码,相信会被它的思想深深的折服。我不建议直接背,但是你理解代码后,也很容易写出来。
int lowbit(int x){ return x&(-x); }
void change(int i, int val){ while(i<c.size()){ c[i]+=val; i+=lowbit(i); } }
void sum(int n){ int sum=0 ; while(n>0){ sum+=c[n]; n-=lowbit(n); } return sum;}
下面将它应用到这道题中。
需要注意的是:上面的change函数,是在原来值的基础上加上一个数,而不是直接替换掉原来的那个数,在update时需要注意。
class NumArray {public: vector<int> sums; vector<int> nums; int lowbit(int x){ return x&(-x); } void change(int i, int vals){ while(i<sums.size()){ sums[i]+=vals; i+=lowbit(i); } } int sum(int n){ int sum=0; while(n>0){ sum+=sums[n]; n-=lowbit(n); } return sum; } NumArray(vector<int> &nums) { this->nums = nums; sums.resize(nums.size()+1); for(int i=0;i<nums.size();i++){ change(i+1,nums[i]); } } void update(int i, int val) { int delta = val-nums[i]; nums[i] = val; change(i+1,delta); } int sumRange(int i, int j) { return sum(j+1)-sum(i); }};
时间消耗会比使用线段树少一点。。。。
Reference Link:
http://www.cppblog.com/Ylemzy/articles/98322.html
http://www.cnblogs.com/zhangshu/archive/2011/08/16/2141396.html
http://www.cnblogs.com/yrbbest/p/5056739.html
http://blog.csdn.net/fly_yr/article/details/50276487
- (LeetCode 307) Range Sum Query - Mutable(树状数组讲解)
- leetcode.307. Range Sum Query - Mutable 树状数组
- [leetcode-307]Range Sum Query - Mutable(java)
- leetcode 307 : Range Sum Query - Mutable
- [LeetCode 307] Range Sum Query - Mutable
- LeetCode(307) Range Sum Query - Mutable
- Leetcode (307) Range Sum Query - Mutable
- [LeetCode]Range Sum Query - Mutable
- Range Sum Query - Mutable -leetcode
- leetcode Range Sum Query - Mutable
- Leetcode: Range Sum Query - Mutable
- Leetcode: Range Sum Query - Mutable
- leetcode, Range Sum Query - Mutable
- LeetCode Range Sum Query - Mutable
- leetcode - Range Sum Query - Mutable
- Range Sum Query - Mutable -- Leetcode
- leetcode Range Sum Query-Mutable
- 【Leetcode】Range Sum Query - Mutable
- Spark性能相关参数配置 之 schedule调度相关
- C count_prime 南京理工大学第八届程序设计大赛
- shell中命令的执行:$( ) 命令
- 第1章 IntentFilter的匹配规则(3)
- android之getevent/sendevent/input
- (LeetCode 307) Range Sum Query - Mutable(树状数组讲解)
- android开发笔记之Android介绍
- EntityManager接口有三种方法来建立原生查询
- Ubuntu下Eclipse安装SVN插件(Subversion & Subclipse)
- Ionic_1 环境部署
- Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://localhost:3306/test'
- java类的初始化
- 数据结构之线性结构(stack、queue、linklist)概念
- 科学美国人60秒:捡垃圾生活让鹳忘却自古流传迁徙习惯