[抄题]:

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. DecimalFormat格式化数字

    DecimalFormat格式化数字 DecimalFormat类也是Format的一个子类,主要作用是格式化数字.当然,在格式化数字时要比直接使用NumberFormat更加 方便,因为可以直接指定 ...

  2. cratedb 集群 docker-compose 安装试用

    关于集群的配置说明可以参考官方文档,或者es 文档 详细代码参考 https://github.com/rongfengliang/cratedb-cluster-docker 参考配置 docker ...

  3. nats 学习 集群ha 配置

      nats 的ha 是一个mesh 的结构,有两个主要的参数 clusters routers 启动三分节点(单机) 共享变量 SERVERS=nats://127.0.0.1:6222,nats: ...

  4. GNU Radio: USRP2 and N2x0 Series

    Comparative features list 相对性能清单 Hardware Capabilities: 1 transceiver card slot External PPS referen ...

  5. GNU Radio: Multiple USRP configurations 配置多个USRP设备

    Introduction 引言 Some USRP devices are capable of being grouped to form a single, virtual device. A s ...

  6. 冒泡排序算法-Python实现

    #-*- coding: UTF-8 -*- import numpy as np def BubbleSort(a): for i in xrange(0, a.size): for j in xr ...

  7. java的按值传递与按引用传递

    还是比较混乱 主要看怎么理解了 java没有指针一说是因为jvm将指针给隐藏了起来 说到底还是靠地址 按值传递显然直接将内存空间的内容传递给对方 之后再与传递者无关 引用是在栈空间建一个堆空间对象的映 ...

  8. ACM-Teleportation

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { int a,b,x,y; cin>>a> ...

  9. 汇编_指令_IRET

    IRET(interrupt return)中断返回,中断服务程序的最后一条指令.   汇编指令IRET [指令格式]IRET   [指令功能]IRET(interrupt return)中断返回,中 ...

  10. Hive默认分割符

    1.Hive默认的分隔符 Hive的表数据,不管导出到HDFS还是本地文件系统,如果用户在导出时没有指定分割符,那么Hive表的数据在写入文件时,会使用默认的分隔符作为列分隔符,该默认的分割是“CTR ...