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. WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [] milliseconds.

    编译安装tomcat-native和tomcat-deamon以后,发现toomcat启动很慢,好久才有响应.以下日志供参考: 11-Sep-2017 12:19:28.102 INFO [main] ...

  2. Macbook pro睡眠状态恢复后没声音的解决办法

    杀招: sudo killall coreaudiod macos会自动重启进程,恢复声音

  3. Linux升级Ruby

    一.简介 Ruby 是一种开源的面向对象程序设计的服务器端脚本语言,在 20 世纪 90 年代中期由日本的松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)设计并开发.在 Ruby 社 ...

  4. illumina SBS测序详解

    illumina SBS测序详解 2018年01月02日 09:33:56 sixu_9days 阅读数:9789 标签: 生物信息学二代测序 更多 个人分类: 测序原理   最近回头重新看了illl ...

  5. IG—金字塔

    博客链接 选择困难症的福音--团队Scrum冲刺阶段-Day 1领航 选择困难症的福音--团队Scrum冲刺阶段-Day 2 选择困难症的福音--团队Scrum冲刺阶段-Day 3 选择困难症的福音- ...

  6. JVM知识

    堆(Heap)和非堆(Non-heap)内存 简单来说堆就是Java代码可及的内存,是留给开发人员使用的:非堆就是JVM留给 自己用的. 堆内存分配     JVM初始分配的内存由-Xms指定,默认是 ...

  7. RocketMQ的客户端连接数调查

    RocketMQ版本:3.4.6 ==问题现象== RocketMQ集群的某个topic,在一部分节点上消费有“断层”,这部分数据一致没办法消费. ==调查过程== 一顿操作猛如虎的调查之后发现, 该 ...

  8. shell的基本语法

    一 赋值运算符 1 += :使用方法是,((x+=需要增加的数字))算和值. 2 *=  :使用方法是,((x*=需要怎加的倍数))算乘值. 3 %= :使用方法是,((x%=需要除以的数字))算余数 ...

  9. TCP协议理解

    一.前言: TCP协议和UDP协议是网络编程里最重要的协议,很多新出的技术.新出的协议本质上都是基于这两个协议的,其中又以TCP协议居多:比如HTTP协议就是基于TCP协议的,应用程序和数据库交互也是 ...

  10. MySQL性能调优与架构设计——第 14 章 可扩展性设计之数据切分

    第 14 章 可扩展性设计之数据切分 前言 通过 MySQL Replication 功能所实现的扩展总是会受到数据库大小的限制,一旦数据库过于庞大,尤其是当写入过于频繁,很难由一台主机支撑的时候,我 ...