题目:

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. for var let闭包理解

    let. var. setTimeout,一点思考. for(var i = 0; i < 10; i++){ setTimeout(function(){ console.log(i); }, ...

  2. 个性化 UIAlertController

    系统的 UIAlertController 封装的很漂亮,用block代替之前 UIAlertView 的代理,用起来更方便的,但是其曝露出来的接口也不多如果要个性化一些东西,比如字体大小.颜色就不是 ...

  3. #leetcode刷题之路43-字符串相乘

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1:输入: num1 = "2", num2 ...

  4. php基础知识考察点:正则表达式

    1.正则表达式的作用:分割,查找,匹配,替换 字符串 2.分隔符:正斜线(/),hash符(#)以及取反符号(~). 3.通用原子:\d \D \s \S \w \W 4.原子符 5. 模式修正符 6 ...

  5. c++ 条件判断

    if语句 认识算术比较 运算符表达式用法 关系运算符表达式 一. 基本的if语句 if (条件成立) { 则执行此语句 }; // ) printf("条件成立"); 二.认识算术 ...

  6. Hbase操作table常见方法示例

    首先上我的输出类: /** * 功能:电池历史数据数据结构 * Created by liuhuichao on 2016/12/5. */ public class ResBatteryDataHi ...

  7. CF248E Piglet's Birthday

    题面 题意翻译 给定$n$个货架,初始时每个上面有$a[i]$个蜜罐. 有$q$次操作,每次操作形如$u,v,k$,表示从货架$u$上任意选择$k$个蜜罐试吃(吃过的也还能吃),吃完后把这$k$个蜜罐 ...

  8. Floodlight下发流表过程分析

    https://blog.csdn.net/vonzhoufz/article/details/32166445 当一个packet到达openflow交换机,会进行流表的匹配,如果没有找到相应的流表 ...

  9. 解决公司的垃圾wifi dhcp获取不到ip 以及配上ip也不能联网的原因

    用手机连公司的wifi时,发现dhcp自动获取不到ip,然后配置了静态ip,但是还是无法联网, 然后发现鸡巴垃圾公司傻逼操她妈的逼原来是google的dns导致不能用???  换成114.114.11 ...

  10. JAVAWEB servlet和jsp的权限访问控制

    要想防止用户直接访问某个页面,那么就需要要求他带着session来访问,我们才让他访问,所以登录时设置的session就用上了 在用户没有session时,访问servlet或者jsp时直接重定向页面 ...