题目

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.

分析

一个可变数组数据结构的实现,包含更新数组元素和求部分和两个函数。

乍一看好像一个很简单的题目,很容易便可以 根据下标O(1)更新元素,由遍历操作O(n)实现求和;但是得到TLE的提交反馈也应该在意料之中。

查阅资料,发现一个树状数组的解法,给出参考博客:网址

树状数组是一种用于通过辅助数组和位置算法实现动态管理部分和的一种算法,其核心函数为lowbit、add、sum,当我们需要加入一个元素时,使用add函数向某个位置注入值,当需要求0~某位置的和时,调用sum函数传入这个位置即可:

 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;
}

其中c就是辅助数组。需要注意的是树状数组的索引必须从1开始,因此在与题目的输入量相转化时,需要把索引+1作为树状数组的索引。

假设我们有数组arr:{1,2,3},要求部分和,我们先把这些值通过add注入树状数组,一定要注意索引+1.

for(int i = 0; i < 3; i++){
add(i+1,arr[i]); // 把arr[i]添加到树状数组的i+1位置
}

操作完成后,树状数组就建立好了,下面就可以利用sum来求和了。例如我们需要arr中索引0~2的和,那么使用sum(3)即可,如果要求1~2的和,可以用sum(3)-sum(0),注意到,sum(0)对于树状数组是一个非法索引,但是通过观察sum函数发现pos=0正好返回0,也就是到这个位置的和为0,满足要求,不必特殊处理。

综上所述,要求arr中i~j的部分和,使用sum(j+1)-sum(i)即可。

值的更新问题,题目要求使用update函数更新arr中位置i的值为val,这就要利用add函数来实现,注意add函数是在位置pos上追加一个值value,而不是覆盖,因此我们需要计算值的变化量,把它追加到相应位置,并且一定要记得更新arr,否则下次得到的变化量是错误的。

void update(int i, int val) {
int ori = m_nums[i]; // m_nums是拷贝arr数组所得的成员变量
int delta = val - ori;
m_nums[i] = val;
add(i+1,delta);
}

所谓树状数组也是第一次了解到,还需查阅资料深入学习一下。

AC代码

//普通方法实现
class NumArray1 {
public:
NumArray1(vector<int> &nums) {
array = vector<int>(nums.begin(), nums.end());
int len = array.size(), tmpSum = 0;
for (int i = 0; i < len; ++i)
{
tmpSum += array[i];
allSum.push_back(tmpSum);
}//for
} void update(int i, int val) {
if (i < 0 || i >= array.size())
return;
int tmp = val - array[i];
array[i] = val;
for (; i < array.size(); ++i)
allSum[i] += tmp;
} int sumRange(int i, int j) {
if (i < 0 || i >= array.size() || j<0 || j >= array.size() || i>j)
return 0;
if (0 == i)
return allSum[j];
else
return allSum[j] - allSum[i - 1];
} private:
vector<int> array;
vector<int> allSum;
}; //树状数组实现
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);
}
};

GitHub测试程序源码

LeetCode(307) Range Sum Query - Mutable的更多相关文章

  1. LeetCode(304)Range Sum Query 2D - Immutable

    题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  2. LeetCode(303)Range Sum Query - Immutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  3. 【刷题-LeetCode】307. Range Sum Query - Mutable

    Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...

  4. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  5. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  6. [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 ...

  7. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  8. leetcode笔记:Range Sum Query - Mutable

    一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  9. 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), ...

随机推荐

  1. NET Core中使用Apworks

    NET Core中使用Apworks HAL,全称为Hypertext Application Language,它是一种简单的数据格式,它能以一种简单.统一的形式,在API中引入超链接特性,使得AP ...

  2. JavaSE---jar文件

    1.当一个应用程序开发完成后,大致有3种方式发布: 1.1 使用平台相关的编译器将整个应用编译成平台相关的可执行文件: 1.2 为整个应用编辑一个批处理文件: 1.3 将应用程序制作为一个可执行的ja ...

  3. 去掉 Ctrl + A 全选

    import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public ...

  4. TDH-常见运维指令

    1.查看cpu: cat /proc/cpuinfo | grep processor2.查看磁盘:df -h (查看磁盘使用率) df -i (查看iNode使用) fdisk -l (查看磁盘整体 ...

  5. CM5.7.2 yum离线安装笔记

    一.建立yum本地服务源(yum支持http和ftp两种协议,这里使用http协议)  1.启动httpd服务   启动命令:service httpd start   关闭命令:service ht ...

  6. AngularJS(一):概述

    本文也同步发表在我的公众号“我的天空” 在我们之前学习的前端代码编写过程中,总是通过HTML与CSS来进行页面布局,而使用JS来控制页面逻辑,因此,我们习惯于在JS中来操作页面元素,如以下代码,我们希 ...

  7. vue使用uglifyjs-webpack-plugin后打包报错

    楼主最新对已做项目进行打包优化,配置了打包环境下去除console.log语句插件---使用uglifyjs-webpack-plugin具体代码如下 npm install uglifyjs-web ...

  8. html的文档设置标记上(格式标记)4-5

    <html> <head> <title>第四课的标题及第五课的标题</title> <meta charset="utf-8" ...

  9. Android 贝塞尔曲线的浅析

    博客也开了挺长时间了,一直都没有来写博客,主要原因是自己懒---此篇博客算是给2017年一个好的开始,同时也给2016年画上一个句点,不留遗憾. 那就让我们正式进入今天的主题:贝塞尔曲线. 首先,让我 ...

  10. Google Play发布App中遇到"多个APK:版本1未提供给任何设备配置使用。"问题的解决方法

    在google play上发布apk,当上传了apk文件,填写了相关的内容信息和介绍图片.图标后,出现“发布应用”始终灰色无法点击,查看原因显示如下问题: 其中支持的设备数量始终显示为0,怀疑是编译出 ...