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].

Follow up:
Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)

思路:构建结果数组res,然后从头到尾扫描一遍nums数组。

res[i]所存的值为nums[0]到nums[i - 1]的乘积,res[0] = 1。

最后从尾到头再扫描一遍nums数组,res[i]这次再乘上nums[i + 1]到nums[n - 1]的值。

所乘上的值可以只由一个变量来计算完成。这里用一个int变量,命名为right,初始为1。

则res[i]乘以right就是最后结果值。要注意的是,每次乘完之后,right要更新为right * nums[i]。

 class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums){
int n = nums.size();
vector<int> res(n, );
for (int i = ; i < n; i++)
res[i] = (i == ? : res[i - ] * nums[i - ]);
int right = ;
for (int i = n - ; i >= ; i--)
{
res[i] *= right;
right *= nums[i];
}
return res;
}
};

Product of Array Except Self - LeetCode的更多相关文章

  1. LeetCode OJ 238. Product of Array Except Self 解题报告

        题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Se ...

  2. LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)

    238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...

  3. 【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 ...

  4. 【LeetCode】238. Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  5. leetcode:238. Product of Array Except Self(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Product of Array Except Self Given an array of n integers where n > ...

  6. 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 ...

  7. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  8. 【刷题-LeetCode】238. Product of Array Except Self

    Product of Array Except Self Given an array nums of n integers where n > 1, return an array outpu ...

  9. [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 ...

随机推荐

  1. greenplum-时间处理

    工作中遇到,需要改变两周以前的数据状态,于是查了下资料,原来数据库直接就可以处理,所以分享给大家! 在PostgreSQL中可以直接对时间进行加减运算:. SELECT now()::timestam ...

  2. D3DXCreateTexture

    HRESULT D3DXCreateTexture( __in LPDIRECT3DDEVICE9 pDevice, __in UINT Width, __in UINT Height, __in U ...

  3. TCP/IP网络编程之套接字类型与协议设置

    套接字与协议 如果相隔很远的两人要进行通话,必须先决定对话方式.如果一方使用电话,另一方也必须使用电话,而不是书信.可以说,电话就是两人对话的协议.协议是对话中使用的通信规则,扩展到计算机领域可整理为 ...

  4. 【Combination Sum II 】cpp

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  5. Web前端知识体系V0.1

    学习,是一个建立“索引”的过程-好比我们读一本书,读完之后,再次看这本书的目录结构,就会联想起很多书中的具体内容: 博客,是一个回顾所学的载体-学习完教学Video之后,通过书写博客,以达到记忆的目的 ...

  6. Anaconda的用法

    1. conda list -----显示anacoda安装的模块 2.jupyter notebook----启动网页上的python编程环境 3.anacoda  search -t conda ...

  7. Spring boot 上传文件大小限制

    1.spring boot 1.x 版本 application.properties 文件中 位置在(resources下) spring.http.multipart.maxFileSize = ...

  8. OV7725学习之SCCB协议(一)

    OV7725摄像头只能作为从机,通过SCCB协议配置内置的172个寄存器.因此首先要了解的就是SCCB总线 1.SCCB协议简述 SCCB协议有两线也有三线,两线为SIO_C与SIO_D,三线为SIO ...

  9. Unity开发VR——Oculus Rif_将Oculus接入Unity

    该文档基于 Unity2018.3.12f1 1. 搭建简单场景 2. 设置,选择 Edit - Project Setting(若已经勾选,就去掉在勾选一次) 完成该步骤之后,可以带上Oculus头 ...

  10. ssh公钥私钥

    ssh配置文件vi /etc/ssh/sshd_config putty下载http://www.chiark.greenend.org.uk/~sgtatham/putty/download.htm ...