题目:

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

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

分析:

题目的意思很简单,给定一个数组,使用sumRange(i,j)函数来计算两个索引之间元素的和。

最简单的方法便是根据给的i,j直接求和。不过在note中我们看到会多次调用sumRange这个函数,如果每次调用都重新求这样时间复杂度会很高。

现在我们开辟一个新的sums数组,大小和nums一样大。其中sums[ i ]表示nums[ i ]之前所有元素的和(包括nums[ i ]这个元素)。

根据样例nums = [-2,0,3,-5,2,-1],我们可以求出sums = [-2,-2,1,-4,-2,-3]。

sums[1] = nums[0] + nums [1] = -2 + 0 = -2

sums[3] = nums[0] + nums [1] + nums[2] + nums [3] = -2 + 0 + 3 + -5 = -4

我们来看当调用sumRange(i,j)时实际上就是要求nums[ i ] + nums[ i+1 ] + ... + nums[ j -1 ] + nums[ j ]的和。

而我们刚才求的sums数组其中:

sums[ i-1 ] = nums[0] + nums[1] + ... + nums[ i-1 ]

sums[ j ] = nums[0] + nums[1] + ... + nums[ j ]

因为i <= j 所以有sums[ j ] - sums[ i-1 ] = nums[ i ] + nums[ i+1 ] + ... + nums[ j -1 ] + nums[ j ]

这样在开始求一次sums数组,便可以在多次调用sumRange事不在重复计算了。可以直接返回sums[ j ] - sums[ i-1 ] 。

注意当i = 0时要做特殊的处理,sumRange(0,j) = sum[ j ],也就是直接返回 sum[ j ]就可以了因为它本身就是nums[ j ]之前所有元素的和。

程序:

/*class NumArray {
public:
NumArray(vector<int> nums) {
myNums.swap(nums);
} int sumRange(int i, int j) {
int sum = 0;
for(int q = i; q <= j; ++q){
sum += myNums[q];
}
return sum;
}
private:
vector<int> myNums;
};*/
class NumArray {
public:
NumArray(vector<int> nums) {
for(auto i:nums){
if(sums.empty())
sums.push_back(i);
else
sums.push_back(sums.back()+i);
}
} int sumRange(int i, int j) {
if(i == )
return sums[j];
else
return sums[j]-sums[i-];
}
private:
vector<int> sums;
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/

LeetCode 303. Range Sum Query - Immutable (C++)的更多相关文章

  1. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

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

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

  3. LeetCode 303. Range Sum Query – Immutable

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

  4. Leetcode 303 Range Sum Query - Immutable

    题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...

  5. Java [Leetcode 303]Range Sum Query - Immutable

    题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...

  6. LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)

    翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...

  7. leetcode 303. Range Sum Query - Immutable(前缀和)

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

  8. 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...

  9. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

随机推荐

  1. windows 下nginx配置php支持

    修改nginx配置 location ~ \.php$ { root D:/Learn/php/test/; fastcgi_pass ; fastcgi_index index.php; fastc ...

  2. Week5:Neural Network BackPropagation疑难点记录

    1.这个neural network 的costfunction 看起来很复杂,其实把连加化简,就是上面的普通代价函数在神经网络的应用,只不过把每一层都加起来了. 为什么要初始化θ值? 后向传播涉及的 ...

  3. P2196 挖地雷

    题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...

  4. PCB直角走线的影响

    PCB直角走线的影响   布线(Layout)是PCB设计工程师最基本的工作技能之一.走线的好坏将直接影响到整个系统的性能,大多数高速的设计理论也要最终经过 Layout 得以实现并验证,由此可见,布 ...

  5. Msys/MinGW与Cygwin/gcc

    一. MinGW MinGW 官方网站为 http://www.mingw.org/ MinGW,即 Minimalist GNU For Windows(GCC compiler suite).它是 ...

  6. ubuntu下安装eclipse IDE for C/C++ developers

     序 linux的GUI和windos比起来实在逊色,虽然它的终端模式(命令行模式)非常强大.linux发行版ubuntu的GUI相对其他版本要华丽一些,所以最近由redhat转向ubuntu进行li ...

  7. jquery,字符串转json对象,json对象转字符串

    字符串转json对象 方法一:var json = eval('(' + str + ')'); 方法二:return JSON.parse(str); json对象转字符串 JSON.stringi ...

  8. ARM Linux驱动篇 学习温度传感器ds18b20的驱动编写过程

    ARM Linux驱动篇 学习温度传感器ds18b20的驱动编写过程 原文地址:http://www.cnblogs.com/NickQ/p/9026545.html 一.开发板与ds18b20的入门 ...

  9. C语言中堆内存的开辟和释放与内存处理函数

    C语言动态分配内存,malloc的出现就是来弥补静态内存分配的缺点 比如说我们在定义数组的时候,数组的长度必须是一个常量,不能改变的值,假如我事先定义了数组,一旦业务需求发生改变,那么这个数组就不能再 ...

  10. VIM - visual selection 模式下的简单操作

    1. 概述 vim 的 visual selection 模式下的简单操作 2. visual selection 模式 概述 可视化选择 可视化选择 vim 的一种专门用来选择的模式 可以提供相对于 ...