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. C语言用一维数组打印杨辉三角(原:无意中想到)

    本贴地址 ] = { }; a[] = , a[] = ; int i, j,m; ; i <= ; i++) //2-11 输出10行 { ; j > ; j--) //关键在这句,倒着 ...

  2. 【Remove Duplicates from Sorted Array II】cpp

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  3. IOS笔记044-通知和代理(观察者模式和代理模式)

      处理文本输入框的输入事件,单击文本输入框后要弹出键盘. 弹出键盘有两种实现方式:一种代理,一种通知.也就是对应的(观察者模式和代理模式).   1.通知 1.1.准备工作 每一个应用程序都有一个通 ...

  4. 【LoadRunner】利用lr_db_connect函数对Oracle数据库压测的完整流程

    项目中常常会有直接对数据库进行压测的需求,以前都是通过Jmeter实现的,但是Jmeter本身图表及结果收集方面没有Loadrunner那么强大,所以利用loadrunner工具自己的函数整理了一个脚 ...

  5. 用户注册,登录API 接口

    Controer: <?php /** * @name UserController * @author pangee * @desc 用户控制器 */ class UserController ...

  6. uploadify 报http 302错误

    uploadify 报http 302错误 原因是系统采用Forms认证,服务端加入匿名认证即可 具体配置如下: <location path="Base/Base/Upload&qu ...

  7. [Python]Pandas简单入门(转)

    本篇文章转自 https://colab.research.google.com/notebooks/mlcc/intro_to_pandas.ipynb?hl=zh-cn#scrollTo=zCOn ...

  8. iOS-绘制图层-CALayer的属性

    一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设 ...

  9. 12小时制时间&&24小时制时间

    今天在获取时间的时候发现,插入到数据库中的时间,其中下午的时间直接显示01,02的样子...查了下资料发现了端倪, java.text.SimpleDateFormat f=new java.text ...

  10. [ZJOI2015][bzoj3924] 幻想乡战略游戏 [动态点分治]

    唉:-(动态点分治的思想真是复杂...... 先码住,再做几道题再来填坑 PS:接下来的Code因为用了倍增lca所以TLE一部分,但是懒得改成RMQ了...... Code: #include< ...