题目:

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. c++—— 函数重载(Overroad)

    5 函数重载(Overroad) 函数重载概念 1 函数重载概念 函数重载(Function Overload) 用同一个函数名定义不同的函数 当函数名和不同的参数搭配时函数的含义不同 2 函数重载的 ...

  2. JVM(三)调优工具

    前言 给一个系统定位问题的时候,知识.经验是关键基础,数据是依据,工具是运用知识处理数据的手段.这里说的数据包括:运行日志.异常堆栈.GC日志.线程快照(threaddump/javacore文件). ...

  3. CentOS中用Nexus搭建maven私服,为Hadoop编译提供本地镜像

    系统: CentOS release 6.6 (Final) Nexus:nexus-2.8.1-bundle.tar.gz,下载地址:https://sonatype-download.global ...

  4. 443 B. Table Tennis

    http://codeforces.com/contest/879/problem/B n people are standing in a line to play table tennis. At ...

  5. Vue.js下载方式及基本概念

    Vue.js 简介 说明及下载 Vue.js使用文档已经写的很完备和详细了,通过以下地址可以查看: https://cn.vuejs.org/v2/guide/ vue.js如果当成一个库来使用,可以 ...

  6. JavaWeb基础—过滤器Filter

    一.概念 JavaWeb三大组件之一(组件都有一个特性,需要在web.xml中配置) 过滤器:会在一组资源(jsp servlet等)的前面执行,可以让请求得到目标资源,也可以终止请求,不再继续 也就 ...

  7. Echarts插件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><% Str ...

  8. # 课下测试补交(ch01)20155337

    课下测试补交(ch01) 1.Amdahl定律说明,我们对系统的某个部分做出重大改进,可以显著获得一个系统的加速比.(B) A .正确 B .错误 解析:Amdahl定律,该定律的主要思想是,当我们对 ...

  9. 查内存命令之free

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页     回到顶级页面:PostgreSQL索引页 [作者 高健@博客园  luckyjackgao@gmail. ...

  10. PyQt5 笔记(02):嵌套布局

    如前一篇笔记,我们还是只讨论两层嵌套布局的情况. 前面的布局有一个缺点:有三个内层布局,则需要三个空部件.那若有十个内层布局呢?显然会让人不舒服. 刚才在玩 Qt Designer 时,发现了一个更好 ...