LeetCode 303.区域检索-数组不可变(accumulate()和for循环差异分析)
给定一个整数数组 nums,求出数组从索引 i 到 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
说明:
- 你可以假设数组不可变。
- 会多次调用 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循环差异分析)的更多相关文章
- Leetcode 307.区域检索-数组可修改
区域检索-数组可修改 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. update(i, val) 函数可以通过将下标为 i 的 ...
- Java实现 LeetCode 303 区域和检索 - 数组不可变
303. 区域和检索 - 数组不可变 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [-2, 0, 3, ...
- 【leetcode 简单】 第七十九题 区域和检索 - 数组不可变
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [-2, 0, 3, -5, 2, -1],求和函数 ...
- [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 ...
- iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)
// // ViewController.swift // Swift-Array // // Created by luorende on 16/9/12. // Copyright © 2016年 ...
- LeetCode--303--区域和检索 - 数组不可变
问题描述: 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [-2, 0, 3, -5, 2, -1 ...
- LeetCode:寻找数组的中心索引【668】
LeetCode:寻找数组的中心索引[668] 题目描述 给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和 ...
- LeetCode:删除排序数组中的重复项||【80】
LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
- LeetCode初级算法--数组01:只出现一次的数字
LeetCode初级算法--数组01:只出现一次的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...
随机推荐
- Understanding the Effective Receptive Field in Deep Convolutional Neural Networks
Understanding the Effective Receptive Field in Deep Convolutional Neural Networks 理解深度卷积神经网络中的有效感受野 ...
- redis系列:集群
1 简介 Redis 集群是Redis 的一个分布式实现,它是一个网状结构,每个节点都通过 TCP 连接跟其他每个节点连接.现在来看看Redis集群实现了哪些目标? 在1000个节点的时候仍能表现得很 ...
- 【android】shape的使用
例子:XML 文件保存在 res/drawable/gradient_box.xml: <?xml version="1.0" encoding="utf-8&qu ...
- sql 根据指定字符截取前面几个字符
1.找到指定字所在的位置并且减去多少是要截取的字符长度 CharIndex('元',product_name)-3) 2.截取 SUBSTRING(product_name, CharIndex('元 ...
- Cactus在jexus上安装
在成功安装完Mono和jexus后(强烈建议Mono 4.2.1以上,jexus 5.6.1 以上,本人测试环境就是Mono 4.2.1和jexus 5.6.1) 第一步: 先配置jexus安装目录下 ...
- 消息队列开发记录笔记-ActiveMQ
1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击a ...
- Pandas——修改DataFrame列名
#生成一个数据框 import pandas as pd a = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9]}) #直接修改:缺点必须写明每 ...
- Nginx+ISS+Redis实现完美负载均衡
前篇文章讲到nginx是使网站采用分布式,对用户的请求采用分布式,分配到不同的服务器上,然后进行同一站点的访问,保证了访问的高效,使用率高,生命期长. 说到ISS,这里重点介绍tomcat,Tomca ...
- web安全-点击劫持
web安全-点击劫持 opacity=0 iframe是目标网站 被内嵌了 1.用户亲手操作 盗取用户 视频 2.用户不知情 >* 引导点击 其实点击的是覆盖在下面opacity=0的ifram ...
- 牛客寒假算法基础集训营4 B applese 走方格
链接:https://ac.nowcoder.com/acm/contest/330/B 构造题,但是有两个特判... 1 2 2 1 然后就水了,血亏 #include<stdio.h&g ...