[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

  1. buildSegmentTree时,start > end,没有区间,不能build;相等时 求和直接赋值
  2. updateSegmentTree时,start == end,就只有一个点,该点值为val。
  3. 求sumRange时,表示到头了,可以直接返回root.sum。针对点来求和,参数应该是root的左右区间。
    root.end == end && root.start == start

[思维问题]:

buildSegmentTree是基于数组的,sumRange是基于树的,所以都要新建函数 (函数名一样 参数不一样)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

从节点类开始,必须要有函数,把树新建起来

buildTree

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. ret.sum要用左右节点的sum值更新 (通用的函数一般换一个名称 不直接用调用的名称)
ret.sum = ret.left.sum + ret.right.sum;
  1. update时,dc分为在左子树更新&在右子树更新
update(root.left, pos, val);
  1. 求和时,有三种情况。

[二刷]:

  1. root新建为空,buildtree的函数在外层

[三刷]:

  1. 树返回的是Node, 左右子树都要重建 不需要分类讨论
  2. update函数是根据插入位置pos和mid的关系来划分的 非左即右用的是if else

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(新建n 增加和删除是lgn) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

分治

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

  1. public static void main (String[] args)方法在整个类里面
  2. root是动态变量,不能被当作静态值传输
// Java program to find largest rectangle with all 1s
// in a binary matrix
import java.io.*;
import java.util.*; class NumArray {
class segmentTreeNode{
int start, end;
segmentTreeNode left, right;
int sum; public segmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
this.left = null;
this.right = null;
this.sum = 0;
}
} //root
segmentTreeNode root = null; public NumArray(int[] nums) {
root = buildTree(nums, 0, nums.length - 1);
} public segmentTreeNode buildTree(int[] nums, int start, int end) {
//cc
if (start > end) return null;
else {
segmentTreeNode root = new segmentTreeNode(start, end);
if (start == end) root.sum = nums[start];
else {
int mid = start + (end - start) / 2;
root.left = buildTree(nums, start, mid);
root.right = buildTree(nums, mid + 1, end);
root.sum = root.left.sum + root.right.sum;
}
return root;
}
} public void update(int i, int val) {
update(root, i, val);
} public void update(segmentTreeNode root, int pos, int val) {
//cc change expression
if (root.start == root.end) root.sum = val;
else {
int mid = root.start + (root.end - root.start) / 2;
if (pos <= mid) {
update(root.left, pos, val);
}
else {
update(root.right, pos, val);
}
root.sum = root.left.sum + root.right.sum;
}
} public int sumRange(int i, int j) {
return sumRange(root, i, j);
} public int sumRange(segmentTreeNode root, int start, int end) {
//cc
if (root.start == start && root.end == end) return root.sum;
else {
int mid = root.start + (root.end - root.start) / 2;
if (end <= mid) return sumRange(root.left, start, end);
else if (start >= mid + 1) return sumRange(root.right, start, end);
else return sumRange(root.left, start, mid) + sumRange(root.right, mid + 1, end);
}
}
// Driver code
public static void main (String[] args)
{ int[] nums = {1, 3, 5}; int start = 0, end = 2;
segmentTreeNode root = new segmentTreeNode(start, end);
//System.out.println("sumRange(start, end) is " +
// sumRange(start, end));
System.out.println("executed");
}
} // Contributed by Prakriti Gupta

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. 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 307. 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】307. Range Sum Query - Mutable

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

  9. [Leetcode Week16]Range Sum Query - Mutable

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

随机推荐

  1. 【转】Javascript中的this

    作者: 阮一峰 日期: 2010年4月30日 this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ ...

  2. windows update失败还原更改,无法开机

    转自  http://jingyan.baidu.com/article/59a015e34c5a73f794886520.html?tj=exp_relate_3&st=2&from ...

  3. react 拖拽排序---原生

    定义css, 两个动画 .drag-up { -webkit-animation: dragup ease 0.2s 1; animation: dragup ease 0.2s 1; -webkit ...

  4. 如何在idea中使用Mybatis-generator插件快速生成代码

    传送门 使用这个插件可以快速生成一些代码,包含 实体类/Mapper接口/*Mapper.xml文件 首先,我们需要搭建一个Maven的项目. 在pom.xml中添加代码 <plugins> ...

  5. 6款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具

    一.检测工具名称汇总 HDTune ATTO Disk Benchmark CrystalDiskMark AS SSD Benchmark Parkdale CrystalDiskInfo 二.各项 ...

  6. nsq 安装试用

    因为是mac 系统安装试用brew install nsq 安装 brew install nsq 组件说明 nsqd 守护进程进行消息的接受,缓存以及传递消息给客户端,需要配置nsqlookupd地 ...

  7. jenkins需要安装的插件

    jenkins plugin 需要安装的插件: •发布插件 Deploy to container Plugin 必须 •Maven插件 Maven Integration plugin必须 •git ...

  8. LCD RGB 控制技术讲解 — 时钟篇(上)

    时序图 下面是LCD RGB 控制的典型时序图  天啊,一下就上这玩意,怎么看??? 其实要解释上面的时序图,我们还需要了解一些LCD的显示过程.所以现在只是有个印象,稍后我们详细讲解. LCD显示流 ...

  9. 黄聪:mysql搬家,直接复制data文件夹(*.MYD,*.MYI,innodb)出错,无法正常显示

    解决办法: 1.复制旧mysql的data文件夹中的数据库到新mysql的data文件夹内. 2.删掉旧的“ib_logfile*”等日志文件,重启MySQL后会自动生成新的日志文件的. 3.复制旧的 ...

  10. 【ZZ】大型数据库应用解决方案总结 | 菜鸟教程

    大型数据库应用解决方案总结 http://www.runoob.com/w3cnote/db-solutions.html