[LeetCode] Range Sum Query - Mutable 题解
题目
思路
一看就是单点更新和区间求和,故用线段树做。
一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者思路是一样的,但是导致TLE,因为区间会把所有都递归一遍,加了个判断,就ok了。
if (idx <= middle) {
this->updateHelper(curIdx << 1, leftIdx, middle, idx, val);
}
else {
this->updateHelper((curIdx << 1) | 1, middle+1, rightIdx, idx, val);
}
实现
//
#include "../PreLoad.h"
class Solution {
public:
class NumArray {
public:
struct Node {
int val;
int sum;
};
vector<Node> nodes;
vector<int> nums;
NumArray(vector<int> nums) {
this->nums = nums;
this->nodes.reserve(4 * nums.size());
for (int i = 1; i <= 4 * nums.size(); i++) {
Node node;
node.val = 0;
node.sum = 0;
this->nodes.push_back(node);
}
this->buildTree(1, 1, (int)nums.size());
}
// 单点更新
void update(int i, int val) {
if (i < 0 || i > this->nums.size()) {
return ;
}
this->updateHelper(1, 1, (int)this->nums.size(), i+1, val);
this->nums[i] = val;
}
int sumRange(int i, int j) {
if (i > j) {
return 0;
}
return this->sumHelper(1, 1, (int)this->nums.size(), i+1, j+1);
}
protected:
void buildTree(int curIdx, int leftIdx, int rightIdx) {
if (leftIdx == rightIdx) {
this->nodes[curIdx].val = this->nums[leftIdx-1];
this->nodes[curIdx].sum = this->nums[leftIdx-1];
return ;
}
else if (leftIdx > rightIdx) {
return ;
}
int middle = (leftIdx + rightIdx) / 2;
this->buildTree(curIdx << 1, leftIdx, middle);
this->buildTree((curIdx << 1) | 1, middle+1, rightIdx);
this->updateFromSon(curIdx);
}
void updateFromSon(int curIdx) {
int leftIdx = curIdx << 1;
int rightIdx = leftIdx | 1;
this->nodes[curIdx].sum = this->nodes[leftIdx].sum + this->nodes[rightIdx].sum;
}
int sumHelper(int curIdx, int leftIdx, int rightIdx, int leftRange, int rightRange) {
// 不在范围内
if (leftIdx > rightRange || rightIdx < leftRange) {
return 0;
}
// 在范围内
if (leftIdx >= leftRange && rightIdx <= rightRange) {
return this->nodes[curIdx].sum;
}
int middle = (leftIdx + rightIdx) / 2;
int left = sumHelper(curIdx << 1, leftIdx, middle, leftRange, rightRange);
int right = sumHelper((curIdx << 1) | 1, middle+1, rightIdx, leftRange, rightRange);
return left + right;
}
void updateHelper(int curIdx, int leftIdx, int rightIdx, int idx, int val) {
if (leftIdx > rightIdx) {
return;
}
if (leftIdx == rightIdx) {
if (idx == leftIdx) {
this->nodes[curIdx].val = val;
this->nodes[curIdx].sum = val;
}
return ;
}
int middle = (leftIdx + rightIdx) / 2;
if (idx <= middle) {
this->updateHelper(curIdx << 1, leftIdx, middle, idx, val);
}
else {
this->updateHelper((curIdx << 1) | 1, middle+1, rightIdx, idx, val);
}
this->updateFromSon(curIdx);
}
};
void test() {
vector<int> nums = {7, 2, 7, 2, 0};
NumArray *obj = new NumArray(nums);
int idx, val;
while (cin >> idx >> val) {
obj->update(idx, val);
int sum = obj->sumRange(0, 4);
cout << "sum: " << sum << endl;
}
}
};
/**
* 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] Range Sum Query - Mutable 题解的更多相关文章
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- Leetcode: Range Sum Query - Mutable && Summary: Segment Tree
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
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 ...
随机推荐
- nRF51800 蓝牙学习 进程记录 1:感想
一直想开一个高大上点的博客,觉得博客园不错,便申请了.一直没时间看,都快忘了,无意间登上提示申请到了.便写个东西看看. 正在学习nRF51822的蓝牙开发板,为了做毕设准备.备考中,一直没时间学,但今 ...
- WebForm 三级联动
三级联动 数据库根据父级代号条件写查询 返回list<>集合 方法一: 创建三个下拉列表: ※AutoPostBack:否发生自动回传到服务器的操作.如果把该属性设置为 TRUE,则启用自 ...
- iOS实现白板、画板功能,有趣的涂鸦工具,已封装,简单快捷使用
一.效果图: 二.选择颜色: 分[固定颜色模式]和[自由取模式]. 三.操作栏功能: 1.撤销:撤销上一步操作,可一直往上进行,直到全部清空. 2.清空:直接清除所有绘画. 3.橡皮擦:去除不要的绘 ...
- 【2(2N+1)魔方阵 】
/* 2(2N+1)魔方阵 */ #include<stdio.h> #include<stdlib.h> #define N 6 #define SWAP(x, y) {in ...
- block、inline、inline-block对比
display:block 1.block元素会独占一行,多个block元素会各种新起一行.默认情况下,block元素宽度自动填满其父元素容器: 2.block元素可以设置width和height属性 ...
- 小机器人自动回复(python,可扩展开发微信公众号的小机器人)
api来之图灵机器人.我们都知道微信公众号可以有自动回复,我们先用python脚本编写一个简单的自动回复的脚本,利用图灵机器人的api. http://www.tuling123.com/help/h ...
- Visual Studio 2017 离线安装方式
Visual Studio, 特别是Visual Studio 2017 通常是一个在线安装程序,如果你在安装过程中失去连接,你可以遇到问题.但是,由于法律原因,微软没有提供完整的可下载的ISO镜像. ...
- FZU 1889 龟兔赛跑
Problem 1889 龟兔赛跑 Accept: 1240 Submit: 1650Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- 关于BOM的理解
BOM提供了很多对象,用于访问浏览器的功能,这些功能与网页内容无关 BOM的核心对象时window,她表示浏览器的一个实例 window的双重角色 1.JS访问浏览器窗口的一个接口 2.ECMA ...
- protocol error, got 'n' as reply type byte
centos6.5上安装redis3.2版本,本地访问redis报错protocol error, got 'n' as reply type byte 解决办法 在redis配置文件redis.co ...