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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
- buildSegmentTree时,start > end,没有区间,不能build;相等时 求和直接赋值
- updateSegmentTree时,start == end,就只有一个点,该点值为val。
- 求sumRange时,表示到头了,可以直接返回root.sum。针对点来求和,参数应该是root的左右区间。
root.end == end && root.start == start
[思维问题]:
buildSegmentTree是基于数组的,sumRange是基于树的,所以都要新建函数 (函数名一样 参数不一样)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
从节点类开始,必须要有函数,把树新建起来
buildTree
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- ret.sum要用左右节点的sum值更新 (通用的函数一般换一个名称 不直接用调用的名称)
ret.sum = ret.left.sum + ret.right.sum;
- update时,dc分为在左子树更新&在右子树更新
update(root.left, pos, val);
- 求和时,有三种情况。
[二刷]:
- root新建为空,buildtree的函数在外层
[三刷]:
- 树返回的是Node, 左右子树都要重建 不需要分类讨论
- update函数是根据插入位置pos和mid的关系来划分的 非左即右用的是if else
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(新建n 增加和删除是lgn) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
分治
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
- public static void main (String[] args)方法在整个类里面
- 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查询求和的范围(可变)的更多相关文章
- 【刷题-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 ...
- 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】307. Range Sum Query - Mutable
题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
随机推荐
- CS231n课程笔记翻译9:卷积神经网络笔记
译者注:本文翻译自斯坦福CS231n课程笔记ConvNet notes,由课程教师Andrej Karpathy授权进行翻译.本篇教程由杜客和猴子翻译完成,堃堃和李艺颖进行校对修改. 原文如下 内容列 ...
- Jenkins配置slave遇到“无法启动该应用程序”的问题
飞测说:最近在负责持续集成相关的工作,我们用的是jenkins+svn+maven+sonar, 今天在用slave这块出现了一个问题,排查了好久才解决,踩过的坑,现在和大家一起看看,希望对大家有帮助 ...
- 解决使用SecureCRT出现的Generic clipboard failure错误【自己亲身经历】
2016年11月8日:[解决办法]把金山词霸卸载了 血的教训浪费了好几个小时 相关文章 1.RecureCRT could not get data from the Clipboard 和SAP快捷 ...
- Visual C++2013 使用技巧
对 Visual Studio 2013 的 IDE 不熟悉.刚用VS 中的 VC++ IDE 进行编程,一些东西用得少,或以后久了不用,怕又忘了.现在慢慢知道点,记录点,以备以后查阅. 1. 记编译 ...
- Linux function: unshare
When a new process is created with the clone() system call, a set of flags is provided which tells t ...
- Java9的新特性
2017.9.21延期了好几次的Java9正式发布,在人工智能的时代,java还能不能持续辉煌是个问题.看看java9的新特性没什么让自己想升级的意愿,因为要么时一些特性用不到,要么时已经有其它方案代 ...
- 什么是HBase(六)性能调优
硬件层面 内存要大,最好是ECC(Error Checking and Correcting),实现内存的动态纠错:CPU要多路(每个路彼此隔离)每个路一个CPU,每个cpu上面一般都是2~12核. ...
- angularJS指令动态加载template
angularJS提供了自定义指令的功能,指令可以定义自己的模板控制器,这个就类似于现在框架的组件,一个指令一般对应一个模板, templateUrl: 'templates/exportTmp.ht ...
- springboot 有关拦截器遇到的问题
最近改造搜索服务,原来是用 ngx + lua 写的,虽然性能很高,带来的问题是可维护性不太方便,不是指lua语言方面,是因为团队就2个开发人员,另外一个开发人员的擅长语言是Java,于是准备将搜索服 ...
- 刷新SQL Server所有视图、函数、存储过程 更多 sql 此脚本用于在删除或添加字段时刷新相关视图,并检查视图、函数、存储过程有效性。 [SQL]代码 --视图、存储过程、函数名称 DECLARE @NAME NVARCHAR(255); --局部游标 DECLARE @CUR CURSOR --自动修改未上状态为旷课 SET @CUR=CURSOR SCROLL DYNAMIC FO
刷新SQL Server所有视图.函数.存储过程 更多 sql 此脚本用于在删除或添加字段时刷新相关视图,并检查视图.函数.存储过程有效性. [SQL]代码 --视图.存储过程.函数名称 DE ...