题目

题目

思路

一看就是单点更新和区间求和,故用线段树做。

一开始没搞清楚,题目给定的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 题解的更多相关文章

  1. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  2. 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 ...

  3. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  4. LeetCode Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...

  5. [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 ...

  6. 【刷题-LeetCode】307. Range Sum Query - Mutable

    Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...

  7. [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 ...

  8. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. [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 ...

随机推荐

  1. js在新页面中返回到上一页浏览的历史位置

    在微信浏览器中浏览页面时,在当前页面中当我们将页面往下滚动到某一个位置时,可能我们就会点击某个链接而页面跳转到了另外一个页面,而当我们又返回到上一个页面时我们会发现那个页面还停留在我们之前浏览的位置, ...

  2. SQL Server-聚焦事务对本地变量、临时表、表变量影响以及日志文件存满时如何收缩(三十一)

    前言 接下来我们将SQL Server基础系列还剩下最后几节内容结束,后续再来讲解SQL Server性能调优,我们开始进入主题. SQL Server事务对本地变量影响 事务对变量影响具体是指什么意 ...

  3. Swift 网络请求数据与解析

    一: Swift 网络数据请求与处理最常用第三方 又有时间出来装天才了,还是在学swift,从中又发现一些问题,这两天上网找博客看问题弄的真的心都累.博客一篇写出来,好多就直接照抄,就没有实质性的把问 ...

  4. jQuery的拾色器

    代码如下 1.js <link href="css/farbtastic.css" rel="stylesheet" /> <script t ...

  5. C++STL笔记

    C++STL 1.vector 向量,长度可变的数组 头文件 #include<vector> 1.1vector的定义 vector<typename> name; 例如: ...

  6. find查找命令

    find # 格式 find [路径] [参数] [表达式] -exec 指令 {} \ ; -{} 代表find找到的文件 -\ 禁止转意 : 表示本行指令结束 # find /sbin -type ...

  7. ajax实现下载功能

    ajax实现下载功能 适用场景:由于点击按钮下载excel响应时间过长,此时间段加入加载样式(灰色层.加载动画): 浏览器弹出下载框后,上面的加载样式去掉.  方     法 :使用jquery.fi ...

  8. linux下apache 的安装

    1.进入work目录下:cd /usr/local/work(如没有则自己新建,命令:mkdir /usr/local/work) 2.在woke目录下从网站下载apache并解压:wget http ...

  9. 纪中集训 Day 0?

    好吧昨天的等到今天才来写,现在超不想刷题,来写下blog吧= = 坐了近10H的火车终于来到了中山市 火车上在看空之境界,等有时间补下动画吧= = 到了宿舍各种不习惯(现在才发现还是母校好QAQ)然后 ...

  10. windows管道

    匿名管道的使用 匿名管道主要用于本地父进程和子进程之间的通信, 在父进程中的话,首先是要创建一个匿名管道, 在创建匿名管道成功后,可以获取到对这个匿名管道的读写句柄, 然后父进程就可以向这个匿名管道中 ...