[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 WITHOUT divide operation.
For A = [1, 2, 3]
, return [6, 3, 2]
.
LeetCode上的原题,请参见我之前的博客Product of Array Except Self。
解法一:
class Solution {
public:
/**
* @param A: Given an integers array A
* @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
vector<long long> productExcludeItself(vector<int> &nums) {
int n = nums.size();
vector<long long> fwd(n, ), bwd(n, ), res(n);
for (int i = ; i < n - ; ++i) {
fwd[i + ] = fwd[i] * nums[i];
}
for (int i = n - ; i > ; --i) {
bwd[i - ] = bwd[i] * nums[i];
}
for (int i = ; i < n; ++i) {
res[i] = fwd[i] * bwd[i];
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param A: Given an integers array A
* @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
*/
vector<long long> productExcludeItself(vector<int> &nums) {
long long n = nums.size(), right = ;
vector<long long> res(n, );
for (int i = ; i < n; ++i) {
res[i] = res[i - ] * nums[i - ];
}
for (int i = n - ; i >= ; --i) {
res[i] *= right;
right *= nums[i];
}
return res;
}
};
[LintCode] Product of Array Except Self 除本身之外的数组之积的更多相关文章
- [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 nums of n integers where n > 1, 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 ...
- Lintcode: Product of Array Exclude Itself
Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B wi ...
- [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 (除自身外序列之积)
题意:给一个序列nums,要求返回一个序列ans,两序列元素个数相同,ans第i个元素就是除了nums[i]之外所有的数相乘之积. 思路:时间O(n),额外空间O(0). 第一次扫一遍,处理nums[ ...
- Java [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] ...
- 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】Product of Array Except Self
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
随机推荐
- DIV+CSS布局
宽度自适应两列布局 <!DOCTYPE html> <html> <head> <meta charset="gbk"> <t ...
- (一)Netty源码学习笔记之概念解读
尊重原创,转载注明出处,原文地址:http://www.cnblogs.com/cishengchongyan/p/6121065.html 博主最近在做网络相关的项目,因此有契机学习netty,先 ...
- #Java编程思想笔记(一)——static
Java编程思想笔记(一)--static 看<Java编程思想>已经有一段时间了,一直以来都把笔记做在印象笔记上,今天开始写博客来记录. 第一篇笔记来写static关键字. static ...
- toArray(),toJson(),hidden([ ]),visible([ ])
toArray() 转换为数组,hidden()不输出的字段 public function index(){ $user = model('User'); $data = $user::)-> ...
- BMW
tc金游世界单登陆允许服务 tc金游世界注册机允许服务 tc金游辅助智能游戏清分允许服务[智能游戏] tc金游辅助挂机王清分允许服务[游辅助挂机清分] tc金游世界王自动打牌允许服务[自动打牌] tc ...
- pip 安装插件慢的解决方法
有的时候在服务器上使用pip3安装插件比较慢. 本人非常痛苦,后经指点使用国内的源就好了...效果杠杠的... 以下为使用方法: pip3 install paramiko -i http://mir ...
- 【Java并发系列01】Thread及ThreadGroup杂谈
img { border: solid black 1px } 一.前言 最近开始学习Java并发编程,把学习过程记录下.估计不是那么系统,主要应该是Java API的介绍(不涉及最基础的概念介绍), ...
- Win10商店东方财富网 UWP版更新,支持平板,PC,手机
东方财富股份有限公司 近日向Win10商店提交了东方财富网V4.1版,这次为广大Win10平台用户带来了期待已久的桌面版本,可谓是良心厂商,值得鼓励和支持.4.1主要更新: 1. 支持桌面Window ...
- 关于P,V操作理解的突破,关于并发设计与并行
今天又找了一篇博客研究P,V操作.. 发现..它有一个变量没有声明.. 我就换了篇博客..http://c.biancheng.net/cpp/html/2600.html 然后就看懂了.. 关键突破 ...
- Java IO 操作(一)
(1)File 类的基础用法 // 1.创建 一个file 对象File file = new File("D:\\aaa");// 2.判断此 file 是否是一个文件夹file ...