[LeetCode] Range Sum Query - Mutable

简介: This problem requires a new data structure --- Segment Tree. You may use this GeeksforGeeks article to get some ideas of it.

This problem requires a new data structure --- Segment Tree. You may use this GeeksforGeeks article to get some ideas of it. However, the code in this article may be too verbose. To solve this problem using segment tree, 2guotou has posted a very nice Java code, which is rewritten below in C++. I try to conform to the LeetCode traditions of defining structures and classes.

 1 struct SegmentTreeNode {
 2     int s, e, sum;
 3     SegmentTreeNode* left;
 4     SegmentTreeNode* right;
 5     SegmentTreeNode(int _s, int _e) : s(_s), e(_e), sum(0), left(NULL), right(NULL) {}
 6 };
 7 
 8 class SegmentTree {
 9 public:
10     SegmentTree(vector<int>& nums) {
11         int n = nums.size();
12         root = buildST(nums, 0, n - 1);
13     }
14 
15     void update(int i, int val) {
16         update(root, i, val);
17     }
18 
19     int sumRange(int i, int j) {
20         return sumRange(root, i, j);
21     }
22 private:
23     SegmentTreeNode* root;
24     SegmentTreeNode* buildST(vector<int>& nums, int s, int e) {
25         if (s > e) return NULL;
26         else {
27             SegmentTreeNode* res = new SegmentTreeNode(s, e);
28             if (s == e) res->sum = nums[s];
29             else {
30                 int m = s + (e - s) / 2;
31                 res->left = buildST(nums, s, m);
32                 res->right = buildST(nums, m + 1, e);
33                 res->sum = res->left->sum + res->right->sum;
34             }
35             return res;
36         }
37     }
38     void update(SegmentTreeNode* root, int i, int val) {
39         if (root->s == root->e) root->sum = val;
40         else {
41             int m = root->s + (root->e - root->s) / 2;
42             if (i <= m) update(root->left, i, val);
43             else update(root->right, i, val);
44             root->sum = root->left->sum + root->right->sum;
45         }
46     }
47     int sumRange(SegmentTreeNode* root, int s, int e) {
48         if (root->s == s && root->e == e) return root->sum;
49         else {
50             int m = root->s + (root->e - root->s) / 2;
51             if (e <= m) return sumRange(root->left, s, e);
52             else if (s >= m + 1) return sumRange(root->right, s, e);
53             else return sumRange(root->left, s, m) + sumRange(root->right, m + 1, e);
54         }
55     }
56 };
57 
58 class NumArray {
59 public:
60     NumArray(vector<int>& nums) {
61         st = new SegmentTree(nums);
62     }
63 
64     void update(int i, int val) {
65         st->update(i, val);
66     }
67 
68     int sumRange(int i, int j) {
69         return st->sumRange(i, j);
70     }
71 private:
72     SegmentTree* st;
73 };
74 
75 
76 // Your NumArray object will be instantiated and called as such:
77 // NumArray numArray(nums);
78 // numArray.sumRange(0, 1);
79 // numArray.update(1, 10);
80 // numArray.sumRange(1, 2);

 

目录
相关文章
LeetCode 307. Range Sum Query - Mutable
update(i, val) 函数可以通过将下标为 i 的数值更新为 val,从而对数列进行修改。
102 0
LeetCode 307. Range Sum Query - Mutable
LeetCode 304. Range Sum Query 2D - Immutable
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。
97 0
LeetCode 304. Range Sum Query 2D - Immutable
|
索引
LeetCode 303. Range Sum Query - Immutable
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。
90 0
LeetCode 303. Range Sum Query - Immutable
LeetCode 201. Bitwise AND of Numbers Range
给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。
68 0
LeetCode 201. Bitwise AND of Numbers Range
|
测试技术 索引 Python
LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611359 翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包括i和j)的所有元素之和。
798 0
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
50 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
99 2
|
28天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
47 7