LeetCode OJ:Product of Array Except Self(除己之外的元素乘积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Solve it without division and in O(n).
For example, given [1,2,3,4], return [24,12,8,6].
由于不允许用除法,而且事件复杂度应该为O(N),可以想象到结果数组中摸个位置的只等于起对应nums所有的左边元素以及右边元素相乘即可,那么分两次,一次从右向左一次从左向右就可以完成乘机的计算:
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> res;
int sz = nums.size();
res.resize(sz);
res[sz - ] = ;
for(int i = sz - ; i >= ; --i){
res[i] = res[i + ] * nums[i + ];
}
int left = ;
for(int i = ; i < sz; ++i){
res[i] *= left;
left *= nums[i];
}
return res;
}
};
LeetCode OJ:Product of Array Except Self(除己之外的元素乘积)的更多相关文章
- [LeetCode] 238. Product of Array Except Self 除本身之外的数组之积
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- 【LeetCode】Product of Array Except Self
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
- C#解leetcode 238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- LN : leetcode 238 Product of Array Except Self
lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...
- [LintCode] Product of Array Except Self 除本身之外的数组之积
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WI ...
- [LeetCode] Product of Array Except Self 除本身之外的数组之积
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- LeetCode 238. Product of Array Except Self (去除自己的数组之积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- leetcode日记 Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- (medium)LeetCode 238.Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
随机推荐
- day9 文件的读取
文件操作 一.打开文件 f = open('歌词.txt','w',encoding='utf-8') # f:文件操作符 文件句柄 文件操作对象 open打开文件是依赖了操作系统提供的途径 操作系统 ...
- jquery jsonp请求错误处理
jQuery将ajax方法进行了封装,jsonp可以轻松实现跨域请求. $.ajax({type: 'GET', url: "<%= SysUtils::TASK_CENTER_URL ...
- is和==
==判断两个对象值是否相等 is表示两个变量是否指向一块对象
- SqlServer分页总结-摘抄
sqlserver2008不支持关键字limit ,所以它的分页sql查询语句将不能用mysql的方式进行,幸好sqlserver2008提供了top,rownumber等关键字,这样就能通过这几个关 ...
- IOS UIApplicationMain函数
对于UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString ...
- 百度feed 寒假实习 一面二面(offer)
一面(1小时) 自我介绍,研究方向,本科学校,家乡等等.. 1 介绍了jdd风险登录的比赛,问题背景,建模,特征构建,特征选择,模型等. 个人觉得介绍项目一定要高大上一点,把自己创新或者有创意的点子讲 ...
- cdoj1329卿学姐与魔法
地址:http://acm.uestc.edu.cn/#/problem/show/1329 题目: 卿学姐与魔法 Time Limit: 1200/800MS (Java/Others) M ...
- hadoop27---netty中handler的执行顺序
Netty是基于Java NIO的网络应用框架. Netty是一个NIO client-server(客户端服务器)框架,使用Netty可以快速开发网络应用,例如服务器和客户端协议.Netty提供了一 ...
- sqlserver索引的原理及索引建立的注意事项小结
聚集索引,数据实际上是按顺序存储的,数据页就在索引页上.就好像参考手册将所有主题按顺序编排一样.一旦找到了所要搜索的数据,就完成了这次搜索,对于非聚集索引,索引是安全独立于数据本身结构的,在索引中找到 ...
- linux下字典生成工具-crunch与rtgen
所谓的密码字典主要是配合密码破解软件所使用,密码字典里包括许多人们习惯性设置的密码.这样可以提高密码破解软件的密码破解成功率和命中率,缩短密码破解的时间.当然,如果一个人密码设置没有规律或很复杂,未包 ...