翻译

给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和。

比如:
给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3 批注:
你能够假定这个数组不会改变。
这里会有非常多次对sumRange函数的调用。

原文

Given an integer array nums, find the sum of the elements between indices i and 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:
You may assume that the array does not change.
There are many calls to sumRange function.

分析

一開始我还以为这个题有多简单呢,就写了以下这个代码:

class NumArray {
public:
vector<int> numArray;
NumArray(vector<int> &nums) {
numArray = nums;
}
int sumRange(int i, int j) {
int sum = 0;
while (i < j) {
sum += numArray[i++];
}
return sum;
}
};

由于不知道须要构造函数干嘛,所以硬生生的加了这么一句进去,事实上全然没用。

功能是实现了。可是面对题目变态的測试用例,果然还是崩了。

至于题目測试用例有多变态,数据来说话:一共214513个字符,当中包含了nums的数据,也包含了函数调用,nums的长度没去算,可是sumRange函数的调用次数高达10000次。

看到这个測试用例,就明确要针对函数调用做优化,一个不错的办法是将全部的返回的值:

从0到1的全部元素之和,
从0到2的全部元素之和,
从0到10000的全部元素之和,
……………… 然后计算i和j之间全部元素之和的时候,(由于包含了i和j),也就是求从0到j的全部元素之和减去从0到i-1的全部元素之和。

所以代码就例如以下所看到的了。可是时间上还是使用了624ms叻。

class NumArray {
public:
map<int, int> rangeSum;
NumArray(vector<int> &nums) {
int sum = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
rangeSum.insert(pair<int, int>(i, sum));
}
} int sumRange(int i, int j) {
return rangeSum[j] - rangeSum[i - 1];
}
};

尽管对底层的详细实现不是非常了解。但由于有索引,所以我自觉用vector的话查找其值来跟高速;并且加入值也更加高速,由于仅仅是一个值而不用是键值对。

详细代码例如以下:

代码

class NumArray {
public:
vector<int> rangeSum; NumArray(vector<int> &nums) {
int sum = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
rangeSum.push_back(sum);
}
} int sumRange(int i, int j) {
return rangeSum[j] - rangeSum[i - 1];
}
};

LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)的更多相关文章

  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 (C++)

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

  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. Redis学习篇(六)之ZSet类型及其操作

    ZADD 作用: 将元素及其分数添加到集合中 语法: ZADD key score membre [score member] 当集合元素已经存在时,再次添加会更新其分数 当score是 +inf 时 ...

  2. FastReport.Net使用:[34]小册子报表(奇偶页)

    打印一份小册子类型的报表,能实现如下要求: ●单独的封面,目录,报表内容,背面 ●奇偶页不同的页边距 ●奇偶页不同的页面/页脚 下面的例子将用到以上3点. 1.奇偶页的实现主要通过报表控件对象的Pri ...

  3. Hibernate.cfg.xml详解

    在搭建Hibernate环境时需要配置Hibernate.cfg.xml配置文件,本文将想详细讲解配置文件的内容. 1.数据库连接信息 配置数据库驱动.(其中name为连接方式,我在这写jdbc的连接 ...

  4. 加密url

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 可以采用 https 证书 双向 加密验证. 加密到 JNI 里面,还是可以通过抓包工具 ...

  5. 【UOJ #105】【APIO2014】Beads and wires

    http://uoj.ac/problem/105 好神的dp啊. 确定一个点为根之后,蓝线只能是竖着的,不能横跨兄弟. 枚举每个点为根进行树形dp是\(O(n^2)\)的,\(f(x,0/1)\)表 ...

  6. [BZOJ3203][SDOI2013]保护出题人(凸包+三分)

    https://www.cnblogs.com/Skyminer/p/6435544.html 先不要急于转化成几何模型,先把式子化到底再对应到几何图形中去. #include<cstdio&g ...

  7. 【随机化】【并查集】Gym - 100851J - Jump

    题意:交互题,有一个长度为n(偶数)的二进制串,你需要猜不超过n+500次猜到它.如果你猜的串与原串相同的位数为n,那么会返回n,如果为n/2,那么会返回n/2,否则都会返回零. 先random,直到 ...

  8. Java Queue的测试

    上传图片没上去,提交的时候已经结束 代码链接

  9. [转] Eclipse的Tomcat插件安装

      Eclipse的Tomcat服务器插件tomcatPlugin是由Sysdeo公司开发的,其下载地址是:http://www.eclipsetotale.com/tomcatPlugin.html ...

  10. [转]Android 中fill_parent与wrap_content的区别

        在Android中,对于组件的属性“layout_width”和“layout_height”, 其值总是设置为“wrap_content”或“fill_parent”. 那么,这两个值有什么 ...