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:

  1. The array is only modifiable by the update function.
  2. You may assume the number of calls to update and sumRange function is distributed evenly.

 
 
 
class NumArray
{
public:
NumArray(vector<int> &nums)
{
sums.push_back();
for (int i = ; i < nums.size(); i++)
{
sums.push_back(sums[i] + nums[i]);
values.push_back(nums[i]);
}
} void update(int i, int val)
{
int diff = val - values[i];
for (int k = i + ; k < sums.size(); k++)
sums[k] = sums[k] + diff;
values[i] = val;
} int sumRange(int i, int j)
{
return sums[j + ] - sums[i];
}
private:
vector<int> sums;
vector<int> values;
};

Binary Indexed Tree-307. Range Sum Query - Mutable的更多相关文章

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

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

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

  3. 307. Range Sum Query - Mutable

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

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

  5. 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 ...

  6. 307. Range Sum Query - Mutable查询求和的范围(可变)

    [抄题]: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...

  7. 【leetcode】307. Range Sum Query - Mutable

    题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...

  8. 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 ...

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

  10. [Leetcode Week16]Range Sum Query - Mutable

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

随机推荐

  1. linux-radhat-gitlab服务搭建

    1.安装gitlab的依赖项 sudo yum install -y curl policycoreutils-python openssh-server cronie 2.设置防火墙 sudo lo ...

  2. phython学习

    Python 中文学习大本营 关于作者 赞助本站 The Python Tutorial (Python 2.7.X) 的中文翻译版本.Python Tutorial 为初学 Python 必备官方教 ...

  3. c3p0数据源配置

    Xml代码   <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: --> ...

  4. Java WebService 知识点汇总

    java webservice 获取传入IP axis.jar    servlet.jar MessageContext mMsgContext = MessageContext.getCurren ...

  5. python的代码检查

    #!/bin/python3.4# coding=utf-8 class lexicon(object): # direction = ['north', 'south', 'east', 'west ...

  6. 201621123008 《Java程序设计》第四周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键字:继承,多态. 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 2. 书面作业 1. ...

  7. APP UI结构-首页功能点大集锦,很干很详细

    APP UI结构的系列的文章有一段时间没有更新了,因为最近在学一些新东西和看一些新书籍,适当的给自己充电也是为了更好的输出,言归正传,今天想跟大家聊的是和首页相关的一些内容,可能有些内容最近有的小伙伴 ...

  8. 多线程的使用:只能用cmd来玩不能用idle

    from multiprocessing import Process import os def pro_do(name, func): print () if __name__ == " ...

  9. 移动文件读/写指针----lseek

    头文件:#include<sys/types.h>.#include<unistd.h> 函数原型:off_t lseek(int fildes,off_t offset,in ...

  10. part1:13-linux编译器GCC

    Gcc特点 Gcc基本用法 1.gcc的概念 GCC(GNU Compiler Collection,GNU编译器套装),是一款由GNU开发的编程语言编译器.GCC原名为GNU C 语言编译器,因为它 ...