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 {

    class STreeNode {
int sum;
int start;
int end;
STreeNode left, right;
STreeNode(int start, int end) {
this.start = start;
this.end = end;
this.sum = 0;
}
} private STreeNode buildSTree(int[] nums, int start, int end) {
if (start > end)
return null;
else {
STreeNode node = new STreeNode(start, end);
if (start == end) {
node.sum = nums[start];
}
else {
int mid = start + (end - start) / 2;
node.left = buildSTree(nums, start, mid);
node.right = buildSTree(nums, mid+1, end);
if (node.left != null && node.right != null)
node.sum = node.left.sum + node.right.sum;
}
return node;
}
} private STreeNode root;
public NumArray(int[] nums) {
root = buildSTree(nums, 0, nums.length-1);
} public void update(int i, int val) {
update(root, i, val);
} private void update(STreeNode node, int pos, int val) {
// if (node == null)
// return;
if (node.start == pos && node.end == pos) {
node.sum = val;
return;
}
if (node.left != null && node.right != null) {
int mid = node.start + (node.end - node.start) / 2;
if (pos <= mid)
update(node.left, pos, val);
else
update(node.right, pos, val); node.sum = node.left.sum + node.right.sum;
} } public int sumRange(int i, int j) {
return sumRange(root, i, j);
} private int sumRange(STreeNode node, int i, int j) {
if (node == null)
return 0;
if (node.start == i && node.end == j)
return node.sum;
int mid = node.start + (node.end - node.start) / 2;
if (j <= mid)
return sumRange(node.left, i, j);
else if (i > mid)
return sumRange(node.right, i, j);
else
return sumRange(node.left, i, mid) + sumRange(node.right, mid+1, j);
} } /**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* obj.update(i,val);
* int param_2 = obj.sumRange(i,j);
*/

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

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

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

  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

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

  6. [Leetcode Week16]Range Sum Query - Mutable

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

  7. 307. Range Sum Query - Mutable

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

  8. leetcode 307 Range Sum Query

    问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ...

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

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

随机推荐

  1. linux CentOS部署【minimal 】

    1.为什么选择CentOS不选择其他版本:http://www.cnblogs.com/TeemoHQ/p/6377260.html 2.准备的资源:VMware[官网下载],CentOS镜像 [阿里 ...

  2. Laravel 验证中文本地化

    1.使用bootsrap好看的提示样式 但是会提示英文 2.将提示中文本地化 2.1.在/resouce/lang下创建文件夹:zh 2.2.已经有小伙伴做好了翻译 https://gist.gith ...

  3. ios 积累

    1.加号 是可以通过类名直接调用这个方法,而减号则要实例化逸个对象,然后通过实例化的对象来调用该方法!! 2.(返回类型) 方法名 :(参数类型)变量名 空格 参数二名 :(参数类型) 变量名 空格 ...

  4. Sencha Touch vs jQuery Mobile

    Sencha Touch:重量级框架,类似于Flex SDK;组件封装较多;在各平台交互表现统一(内部封装);入门门槛较高 jQuery Mobile:轻量级框架,实质是jQuery插件;组件较少;交 ...

  5. 底部粘连(stiky footer)布局

    前面的话 在网页设计中,Sticky footers设计是最古老和最常见的效果之一,大多数人都曾经经历过.它可以概括如下:如果页面内容不够长的时候,页脚块粘贴在视窗底部:如果内容足够长时,页脚块会被内 ...

  6. Linkin大话PC常用快捷键

    不管是不是程序员,常用的键盘的快捷键还是要会的,以下整理一些最常用的也比较重要的PC快捷键. 复制:CTRL+C 剪切:CTRL+X 粘贴:CTRL+V 全选:CTRL+A 撤销键:CTRL+Z 切换 ...

  7. 查阅API文档

    Java的API文档:就一句话:应用程序接口 •API (Application Programming Interface,应用程序编程接口)是 Java 提供的基本编程接口. •Java语言提供了 ...

  8. MyEclipse中阿里JAVA代码规范插件(P3C)的安装及使用

    JAVA代码规范插件(P3C)是阿里巴巴2017年10月14日在杭州云栖大会上首发的,使之前的阿里巴巴JAVA开发手册正式以插件形式公开走向业界.插件的相关信息及安装包都可以在GitHub(https ...

  9. SVN同步时忽略特定文件或文件夹

    在MyEclipse中使用SVN同步的时候,经常会提示一些比如.classpath等不需要同步的配置文件,可以通过设置来忽略这一部分的文件或者文件夹. 1.选择菜单Window→Preferences ...

  10. IO (三)

    1 转换流 1.1 InputStreamReader 1.1.1 InputStreamReader简介 InputStreamReader是字节流通向字符流的桥梁.它使用指定的charset读取字 ...