给定一个整数数组  nums,求出数组从索引 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点。

示例:

给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange()

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

说明:

  1. 你可以假设数组不可变。
  2. 会多次调用 sumRange 方法。
#include <vector>
#include <iostream>
#include <numeric> using namespace std; static int x = []() {std::ios::sync_with_stdio(false); cin.tie(); return ; }();
class NumArray {
public:
NumArray(vector<int> nums) {
sumnums = nums;
} int sumRange(int i, int j) {
return accumulate(sumnums.begin()+i, sumnums.begin()+j+, );
}
private:
vector<int> sumnums;
}; int main()
{
vector<int> vec = {-, , , -, , -};
NumArray *A = new NumArray(vec);
cout << A->sumRange(, ); return ;
}
#include <vector>
#include <iostream>
#include <numeric> using namespace std; static int x = []() {std::ios::sync_with_stdio(false); cin.tie(); return ; }();
class NumArray {
public:
NumArray(vector<int> nums) {
for(int i = ; i < nums.size(); ++i){
nums[i] += nums[i-];
}
sumnums = nums;
} int sumRange(int i, int j) {
if(i == )
return sumnums[j];
return sumnums[j] - sumnums[i-];
}
private:
vector<int> sumnums;
}; int main()
{
vector<int> vec = {-, , , -, , -};
NumArray *A = new NumArray(vec);
cout << A->sumRange(, ); return ;
}

上面是accumulate()下面是for循环

template <class InputIterator, class T>
T accumulate (InputIterator first, InputIterator last, T init)
{
while (first!=last) {
init = init + *first; // or: init=binary_op(init,*first) for the binary_op version
++first;
}
return init;
}

时间复杂度同是O(n),耗时差这么多。

举个栗子

#include <iostream>
#include <vector>
#include <numeric> using namespace std; vector<int> vec = { , , -, };
int sum; void func1()
{
for (int i = ; i < vec.size(); ++i) {
sum += vec[i];
}
} void func2()
{
accumulate(vec.begin(), vec.end(), );
} int main()
{ return ;
}

直接查看汇编代码

首先我怀疑编译器在进行begin()操作或者使用容器时,耗费了时间。

void func1()
{
for (int i = ; i < vec.size(); ++i) {
sum += *(vec.begin()+i);
}
}

同时将LeetCode上for循环数组形式改为begin()形式。

得到汇编代码和运行结果

并没有影响,那么唯一的可能性是,在调用accumulate()时使用了其他内联函数,或者编译器进行了数据资源的申请。

LeetCode 303.区域检索-数组不可变(accumulate()和for循环差异分析)的更多相关文章

  1. Leetcode 307.区域检索-数组可修改

    区域检索-数组可修改 给定一个整数数组  nums,求出数组从索引 i 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点. update(i, val) 函数可以通过将下标为 i 的 ...

  2. Java实现 LeetCode 303 区域和检索 - 数组不可变

    303. 区域和检索 - 数组不可变 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [-2, 0, 3, ...

  3. 【leetcode 简单】 第七十九题 区域和检索 - 数组不可变

    给定一个整数数组  nums,求出数组从索引 i 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点. 示例: 给定 nums = [-2, 0, 3, -5, 2, -1],求和函数 ...

  4. [Swift]LeetCode303. 区域和检索 - 数组不可变 | Range Sum Query - Immutable

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

  5. iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)

    // // ViewController.swift // Swift-Array // // Created by luorende on 16/9/12. // Copyright © 2016年 ...

  6. LeetCode--303--区域和检索 - 数组不可变

    问题描述: 给定一个整数数组  nums,求出数组从索引 i 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点. 示例: 给定 nums = [-2, 0, 3, -5, 2, -1 ...

  7. LeetCode:寻找数组的中心索引【668】

    LeetCode:寻找数组的中心索引[668] 题目描述 给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和 ...

  8. LeetCode:删除排序数组中的重复项||【80】

    LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...

  9. LeetCode初级算法--数组01:只出现一次的数字

    LeetCode初级算法--数组01:只出现一次的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...

随机推荐

  1. Spring @ResponseBody 返回乱码 的优雅解决办法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 返回的结果中,中文全部被问号(?)代替的解决办法: *-servlet.xml的部分配置如下: <bean id=&quo ...

  2. table 和 div 简单布局

    table 简单布局 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  3. timestamp 在curl中变成了Xtamp

    目前的解决方案 将timestemp放在数组最前面. [注意:请求的参数中需要将timestamp这个参数放在数组的最前面,不然在GET方式请求中,会出现浏览器将它变成Xtamp,最终导致签名失败]

  4. oracle修改连接数

    使用 sqlplus登陆   sqlplus system 然后切换到sysdba模式   conn ?/ as sysdba   查询当前的processes sessions的大小   show ...

  5. SQLException: Incorrect string value: '\xE4\xB8\xAD\xE5\x9B\xBD' for column at row 1

    这个是由于新建数据库没有选择默认字符集导致的,只要选择utf8即可. 如果以上还无法解决,那可能是表里的varchar字符集也不对

  6. 暴力【bzoj2208】: [Jsoi2010]连通数

    2208: [Jsoi2010]连通数 暴力过的. 没脸说... 正解好像是缩点+递推. 应该也不难写. code: #include <iostream> #include <cs ...

  7. jq 使用手册

    翻译整理:Young.J官方网站:http://jquery.com jQuery是一款同prototype一样优秀js开发库类,特别是对css和XPath的支持,使我们写js变得更加方便!如果你不是 ...

  8. [HAOI2012]音量调节 BZOJ2748 dp

    题目描述 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都需要改变一次音量.在演出开始之前,他已经做好一个列表,里面写着每首歌开始之前他想要改变的音量是多少. ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  10. nginx的配置文件(反向代理和主配置)

    配置文件保存,是为了工作方便.特别是优化好的配置.我们基本可以直接复制粘贴.这样做可以快速的完成手上的工作!! vim nginx.conf user nginx; worker_processes ...