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 ...
随机推荐
- grub2 详解
grub2详解(翻译和整理官方手册) 分类: Linux 基础篇,Linux 杂项 本文原创地址在博客园:https://www.cnblogs.com/f-ck-need-u/archive ...
- git 基础学习笔记
配置ssh 打开命令行或者git bash 输入 创建git库 git init 检查当前状态 $ git status On branch master nothing to commit, wor ...
- 微信小程序页面跳转的四种方法
wx.navigateTo({}) ,保留当前页面,跳转到应用内的某个页面,使用 wx.navigateBack 可以返回; 示例: 1 wx.navigateTo({ 2 url:'../test/ ...
- train validation test
http://stats.stackexchange.com/questions/19048/what-is-the-difference-between-test-set-and-validatio ...
- Windows 2008 关闭远程桌面的单用户多会话模式
Windows 2008 关闭远程桌面的单用户多会话模式 在腾讯云上购买了一台云服务器. 因为设置了自动登录,在远程桌面连接后会启动一个新的会话,然后软件被运行了两次,端口被占用,无法起动. 还有可能 ...
- tomcat自启动脚本
1.#cd /etc/rc.d/init.d2.#vi tomcat3.把下面的代码保存为tomcat文件,并让它成为可执行文件 chmod 755 tomcat. #!/bin/sh # # /et ...
- centos6.5下tomcat安装
1.安装JDK 安装:rpm –ivh jdk-7u5-linux-i586.rpm2.配置Tomcat 解压:tar -zxvf apache-tomcat-8.0.11.tar.gz 移动到/us ...
- js基础篇(dom操作,字符串,this等)
首先我们来看这样一道题 <div id='foo' class='aa bb cc'></div>写出如何判断此div中是否含有aa(注:aa成立,aaa不成立) 首先,我们一 ...
- 根据现有表操作基于active record的model
指南上都是直接生成mode,然后db migrate来生成数据库,在现实场景中,很可能是反过来的 例如 测试表app_versions rails里面,建立model class AppVersion ...
- vim自定义配置之UI设置
vimConfig/plugin/ui-setting.vim let os = substitute(system('uname'), "\n", "", & ...