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.
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:
- The array is only modifiable by the update function.
- 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的更多相关文章
- [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
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- 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
问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ...
- 【leetcode】307. Range Sum Query - Mutable
题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...
随机推荐
- Sublime Text 使用介绍、全套快捷键及插件推荐
开篇:如果说Notepad++是一款不错Code神器,那么Sublime Text应当称得上是神器滴哥.Sublime Text最大的优点就是跨平台,Mac和Windows均可完美使用:其次是强大的插 ...
- php ueditor 后台配置项返回格式出错,上传功能将不能正常使用!
解决常见的有两种 1,可能是时区设置问题,有系统区分大小写. date_default_timezone_set("Asia/chongqing");改为 date_default ...
- git gui提交无法获知你的身份 20
刚刚学习,请说的详细一些,谢谢 callct | 浏览 3382 次 我有更好的答案 1条回答 你没有定义你的名字和邮箱.你打开git console/shell, #输入下面两句,并且替换成你的名字 ...
- Sublime 安装、插件CoolFormat
http://www.sublimetext.com/3 安装Package Control https://packagecontrol.io/installation#st3 安装插件Cool F ...
- JAVA BASE64
Base64编码说明: Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式. 如果剩下的字符不足3个字节, ...
- servlet多线程问题
Servlet本身是单实例的,这样当多个用户同时访问某个Servlet时,会访问该唯一的Servlet实例中的成员变量,如果对成员变量进行写入工作,那就会导致Servlet的多线程问题,即数据不一致. ...
- 配置shiro错误
在web配置工程中配置shiro,如果启动Tomcat,报错:org.apache.shiro.web.config.WebIniSecurityManagerFactory.setDefaults ...
- LinkedList 源码分析(JDK 1.8)
1.概述 LinkedList 是 Java 集合框架中一个重要的实现,其底层采用的双向链表结构.和 ArrayList 一样,LinkedList 也支持空值和重复值.由于 LinkedList 基 ...
- 2017-06-19 (cp mkdir rm 运行级别及修改)
mkdir 用于创建目录 mkdir -p 递归创建目录 mkdir -p /linux/linux rm 用于删除文件与目录 rm -r 删除目录 -f 强制删除 (一般情况下 rf 组 ...
- Centos7-卸载自带的jdk 安装jdk8
卸载JDK Centos7一般都会带有自己的openjdk,我们一般都回用oracle的jdk,所以要卸载 步骤一:查询系统是否以安装jdk #rpm -qa|grep java 或 #rpm ...