给定一个整数数组  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. springcloud +spring security多种验证方式之第三方token生成自己的token通过校验和自己的表单验证大体流程

    步骤: 1.继承 WebSecurityConfigurerAdapter.class,其中使用两个过滤器,一个spring scurity自带的UsernamePasswordAuthenticat ...

  2. 国内物联网平台(3):QQ物联智能硬件开放平台

    国内物联网平台(3)——QQ物联·智能硬件开放平台 马智 平台定位 将QQ帐号体系.好友关系链.QQ消息通道及音视频服务等核心能力提供给可穿戴设备.智能家居.智能车载.传统硬件等领域的合作伙伴,实现用 ...

  3. 使用canvas压缩图片 并上传

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 通过ajax把json对象传入后台

    一.前台ajax部分 function icheckDelete(url){ var parms = { list : array //这是个数组 }; $.ajax({ dataType: &quo ...

  5. (原创)Problem F: WPF的三位数

    Description PF哥是一个爱说骚话的骚年,今天他决定要用阿拉伯数字来说骚话,他将1,2,…,9共9个数字分成了三组,分别组成三个三位数,且使这三个三位数构成1:2:3的比例 他要说的骚话就是 ...

  6. [SinGuaRiTy] 2017-07-24 NOIP2015 模拟赛

    [SinGuLaRiTy-1030] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有题目: Time Limit: 1s | Mem ...

  7. STL_ALGORITHM_H

    sort_unique_copy /////////////////////////////////////////////////////////// // Copyright (c) 2013, ...

  8. MySQL数据库之插入显示图片

    图书馆系统项目需要用到好多图片,并且要求存入到数据库中,对这个特别感兴趣,于是上网查了资料,采用C#语言,进行了具体实现. 说明: 功能:往MySQL数据库插入并显示图片: 验证:执行插入功能后,我把 ...

  9. SQL sum和group by HAVING

    Aggregate functions (like SUM) often need an added GROUP BY functionality. 集合函数(类似SUM)经常需要用GROUP BY来 ...

  10. uoj#119. 【UR #8】决战圆锥曲线(线段树+复杂度分析)

    题解 传送门 题解 然而要我来说我感觉只是个爆搜啊-- //minamoto #include<bits/stdc++.h> #define R register #define ll l ...