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): " ...
随机推荐
- 使用django建博客时遇到的URLcon相关错误以及解决方法。错误提示:类型错误:include0获得一个意外的关键参数app_name
root@nanlyvm:/home/mydj/mysite# python manage.py runserver Performing system checks... Unhandled exc ...
- 百度编辑器ueditor
,怎么将上传的图片路径改到项目的public/uploads文件夹呢?哪位大神改过
- 认识Java(2)
注释 对程序的一段文字描述 可方便其他用户的阅读,增加代码的可读性.可以注销掉代码,等需要的时候再用. 编译器会自动忽视被注释的内容. 分类: 单行注释 // 多行注释 /* */ 文档注释/** * ...
- util包就是用来放一些公用方法和数据结构的
util包就是用来放一些公用方法和数据结构的
- 怎样用PS对照片进行美白?
摘录自:http://product.pconline.com.cn/itbk/software/ps/1408/5336118.html 步骤1.打开需要美白肤色的照片.本教程为防止侵犯他人肖像权, ...
- jquery 遍历表格,需要表格中每个td的内容
$("table tr:gt(0)").each(function(i){ alert("这是第"+i+"行内容"); $(this).ch ...
- requireJs 踩的坑
<!-- RequireJS --> <script src="assets/js/require.min.js" data-main="assets/ ...
- xampp使用中mysql端口被占用问题的解决方案
如果在安装XAMPP前本机已经安装了mysql,并且添加了Windows服务中 使用xampp时,两个Mysql在Windows服务中有冲突 这意味着你之前在电脑上使用过mysql,路径.端口都被占用 ...
- spring boot 文件上传 文件过大 FileUploadBase$SizeLimitExceed
application.properties中加入 multipart.maxFileSizemultipart.maxRequestSize Spring Boot 1.3.x或者之前 multip ...
- 如何通过命令或脚本方式在Windows上访问linux系统
很多情况下,我们需要在Windows上写脚本,创建计划任务程序,这个过程中可能需要访问linux系统,执行脚本或者上传下载文件.并且我们也不想在Windows上安装什么东西.那最好的办法就是使用put ...