Binary Indexed Tree-307. Range Sum Query - Mutable
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.
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的更多相关文章
- 【刷题-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), inclusive ...
- 307. Range Sum Query - Mutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- [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 ...
- 307. Range Sum Query - Mutable查询求和的范围(可变)
[抄题]: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...
- 【leetcode】307. Range Sum Query - Mutable
题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...
- 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 Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
随机推荐
- OpenGLES.Functions.Missing.in.OpenGLES1.x
转载自: http://maniacdev.com/2009/05/big-list-of-opengl-functions-missing-in-iphone-opengl-es The funct ...
- Banner设计的视觉导向原则分析
Banner的布局方式 常见的Banner布局方式有五种,以下分别给出大致样式: 两栏式: 三栏式: 组合式: 一体式: 对角线式: 以上几种布局方式不分好坏,对于不同的主题.素材和文案可以自行选 ...
- Hibernate不能实时获取MySQL数据库的更新
在hibernate.cfg.xml配置文件中,增加以下内容: <property name="hibernate.connection.provider_class"> ...
- java利用递归实现汉诺塔算法
package 汉诺塔; //引入Scanner包,用于用户输入 import java.util.Scanner; public class 汉诺塔算法 { public static void m ...
- 20155210 2016-2017-2 《Java程序设计》第7周学习总结
20155210 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量: GMT(Greenwich Mean Time)时间:现在不是标准时间 世界时 ...
- 2018.09.22 atcoder Integers on a Tree(构造)
传送门 先考虑什么时候不合法. 第一是考虑任意两个特殊点的权值的奇偶性是否满足条件. 第二是考虑每个点的取值范围是否合法. 如果上述条件都满足的话就可以随便构造出一组解. 代码: #include&l ...
- 2018.07.22哨戒炮 II(树形dp)
哨戒炮 II 描述 你的防线成功升级,从原来的一根线变成了一棵树.这棵树有 N 个炮台,炮台与炮台之间 有 N-1 条隧道.你要选择一些炮台安装哨戒炮.在第 i 个炮台上安装哨戒炮得到的防御力为 vi ...
- Django模型层(1)
https://www.cnblogs.com/yuanchenqi/articles/8933283.html MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦, ...
- UVa 11636 Hello World! (水题思维)
题意:给你一个数,让你求需要复制粘贴多少次才能达到这个数. 析:这真是一个水题,相当水,很容易知道每次都翻倍,只要大于等于给定的数就ok了. 代码如下: #include <iostream&g ...
- linux上安装maven
解压安装: tar -zxvf apache-maven-3.3.9-bin.tar.gz 配置环境变量 cd /etc/ vi profile 在最后面加上M2_HOME=/usr/local/my ...