[LeetCode] Range Sum Query - Immutable

简介: The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums such that accu[i] = nums[0] + .

The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums such that accu[i] = nums[0] + ... + nums[i] in the initializer of NumArray. Then just return accu[j + 1] - accu[i] in sumRange. You may try the example in the problem statement to convince yourself of this idea.

The code is as follows.


C++

 1 class NumArray {
 2 public:
 3     NumArray(vector<int> &nums) {
 4         accu.push_back(0);
 5         for (int num : nums)
 6             accu.push_back(accu.back() + num);
 7     }
 8 
 9     int sumRange(int i, int j) {
10         return accu[j + 1] - accu[i];
11     }
12 private:
13     vector<int> accu;
14 };
15 
16 
17 // Your NumArray object will be instantiated and called as such:
18 // NumArray numArray(nums);
19 // numArray.sumRange(0, 1);
20 // numArray.sumRange(1, 2); 

Python

class NumArray(object):
    def __init__(self, nums):
        """
        initialize your data structure here.
        :type nums: List[int]
        """
        self.accu = [0]
        for num in nums:
            self.accu += self.accu[-1] + num,

    def sumRange(self, i, j):
        """
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int 
        """
        return self.accu[j + 1] - self.accu[i]


# Your NumArray object will be instantiated and called as such:
# numArray = NumArray(nums)
# numArray.sumRange(0, 1)
# 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 307 Range Sum Query - Mutable(范围和查询-可变)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51782135 翻译 给定一个整型数组nums,找出在索引i到j之间的元素的和(i 9 update(1, 2) sumRange(0, 2) -> 8 备注: 该数组只能被update函数修改。
927 0
|
算法
LeetCode 304 Range Sum Query 2D - Immutable(范围求和2D - 不可变)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51783388 翻译 给定一个2D矩阵matrix,找出其中以左上角(row1,col1)和右下角(row2,col2)定义的矩形边界的元素的和。
918 0
|
测试技术 索引 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