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): " ...
随机推荐
- 如何让低版本IE浏览器支持HTML5标签并为其设置样式
现代的浏览器都支持HTML5,HTML5定义了 8 个新的 HTML 语义元素.所有这些元素都是 块级 元素. 为了能让旧版本的浏览器正确显示这些元素,你可以设置 CSS 的 display 属性值为 ...
- JAVA 键盘输入数组,输出数组内容和最大值、最小值
package shuzu; import java.util.Scanner; import java.util.Arrays; public class shuzu { /** * @param ...
- html_栏目下拉
========================================================= =================[ 下拉栏目菜单 ]=============== ...
- 《并行程序设计导论》——MPI(Microsoft MPI)(6):并行排序算法
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
- linux 中nvme 的中断申请及处理
/** * struct irq_desc - interrupt descriptor * @irq_data: per irq and chip data passed down to chip ...
- linux 动态库的符号冲突问题
最近,给同事定位了一个符号表的冲突问题,简单记录一下. A代码作为静态链接库,被包含进了B代码,然后编译成了动态链接库,B.so A代码同时作为静态链接库,被编译进入了main的主代码. main函数 ...
- linkinFrame--web应用举例--准备工作
OK,前面的博客我们已经搭好了linkinFrame的项目结构,现在我们这里添加一个简单的web应用,在编写此web应用的过程中我们来一步一步的搭好自己的框架. 现在开始,这里举一个例子,一个客户的C ...
- junit测试套件
在实际项目中,随着项目进度的开展,单元测试类会越来越多,可是直到现在我们还只会一个一个的单独运行测试类,这在实际项目实践中肯定是不可行的.为了解决这个问题,JUnit 提供了一种批量运行测试类的方法, ...
- maven-assembly-plugin插件的使用方法
一. Assembly 是什么意思? 二. maven-assembly-plugin是什么? 它是maven中针对打包任务而提供的标准插件. 三. maven-assembly-plugin插件的作 ...
- SSMS 2005 连接 SQL SERVER 2008问题
用本机的 Microsoft SQL Server Management Studio 2005 客户端连接数据库服务器时报错:"This version of Microsoft SQL ...