给定一个整数数组  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. python 爬虫之requests+日志+配置文件读取+mysql入库

    #!/usr/bin/env python # -*- coding: utf-8 -*- # 日志管理 import logging import sys reload(sys) sys.setde ...

  2. SQL Server 2014 清理日志

    USE [master] GO ALTER DATABASE [TempTestDb02] SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE [Te ...

  3. 设置datalist指定行的背景色

    前台: <div class="table-responsive" > <table class="table table-bordered table ...

  4. sqlserver快速删除整个表数据

    --删除整个表数据 SET STATISTICS TIME ON; DECLARE @Timer DATETIME = GETDATE(); TRUNCATE TABLE LOG_DEBUG_ERRO ...

  5. 对Dapper的一点改造

    微软推出的ORM, EF在我开发的项目中给我的感觉一直都是慢.优点是高度封装的底层.便于开发. Dapper在多篇性能比较的网站中.都是名列前三.缺点是手写SQL,不便于开发.   如果能结合EF的优 ...

  6. KDevelop4调试pcl一直读取不到.pcd文件

    如题所示,KD下,能编译,运行时一直使用reader.read读取不到pcd,但是使用cmake能正常运行. 后来,使用terminator删掉工程的build文件夹,直接在工程文件下进行编译,报错提 ...

  7. 读《JavaScript权威指南》笔记(三)--对象

    1.对象介绍 对象是JavaScript的基本数据类型.对象是一种复合值:它将很多值(原始值或者其他对象)聚合在一起,可通过名字访问这些值.对象也可看做是属性的无序集合,每个属性都是一个名/值对.属性 ...

  8. AI-Info-Micron-Insight:工业 5.0,伟大的思想将殊途同归

    ylbtech-AI-Info-Micron-Insight:工业 5.0,伟大的思想将殊途同归 1.返回顶部 1. 工业 5.0,伟大的思想将殊途同归 两个头脑比一个好吗?似乎如此,尤其是当其中一个 ...

  9. Rabbitmq——实现消费端限流 --NACK重回队列

    如果是高并发下,rabbitmq服务器上收到成千上万条消息,那么当打开消费端时,这些消息必定喷涌而来,导致消费端消费不过来甚至挂掉都有可能. 在非自动确认的模式下,可以采用限流模式,rabbitmq ...

  10. Mysql安装(Mac)

    1.安装mysql(百度详解) 2.打开终端 3.输入vim ~/.bash_profile 4.在最后加上PATH=$PATH:/usr/local/mysql/bin 5.按esc,然后输入 :w ...