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. ...
随机推荐
- WebGoat系列实验Authentication Flaws
WebGoat系列实验Authentication Flaws Forgot Password Web应用经常给用户提供取回密码的功能,但是许多应用的实现策略实现的很差,用于验证用户的信息非常简单. ...
- C/C++中char* p = "hello" 和 const char* p = "hello"的区别
在写代码常常都会写char * p ="hello";这样的代码,虽然不是错误,但却不建议这样用.应该加const修饰.这句话背后的内涵是什么?下面就刨根问底一下:) 这个行为在不 ...
- C#根据弹窗标题获取窗体句柄并模拟点击按钮(FindWindow,FindWindowEx,SendMessage)
任务:将下面弹窗自动关闭 /// <summary> /// 找到窗口 /// </summary> /// <param name="lpClassName& ...
- ASP.NET MVC 之各种jQuery提交模式实例
1.$.ajax提交 var _data = { "dictItemID": dictItemID, "itemType": itemType, "i ...
- linux文件链接
我的github,欢迎关注,分享知识与技术 链接:一种在共享文件和访问它的用户的若干目录项之间建立联系的一种方法. Linux中包括两种链接:硬链接(HardLink)和软链接(Soft Link), ...
- Spring Boot 学习系列(02)—使用热部署,提升开发效
此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 开发调试很简单 热部署的使用非常简单,但能极大的提高我们的开发效率,像传统的web应用,我们修改后需要重新编 ...
- Oracle PL/SQL编程语法
--plsql块结构,计算a,b的和 declare a ; b ; c int; begin c:=a+b; dbms_output.put_line(c); end; --%type数据类型,输出 ...
- Java基础之对包,类,方法,变量理解(灵感)
包,类,方法,变量 灵感乍现 感觉就如电脑上的各个大小文档一般,只不过名称不同,用法不同,功效不同,就好比你要调用网上的一个图片,这个图片可以是变量,可以是方法,可以是类.你要调用可以把他幻化成接口, ...
- SQL基础(一)
经过这段时间对SQL的基础学习,下面对自己的学习做个总结或者也可以说是个回顾吧! 我练习的是在oracle数据库平台上,并且安装了PLSQL Developer工具.下面是我从小白开始一路学习的回顾: ...
- cookie和session的使用和区别
cookie:存储在浏览器 存值:setcookie("名字",值,过期时间.秒,哪一个文件夹)//文件夹不写一般默认整个网站都可以 setcookie("usernam ...