[LinkedIn] Array of products of all other numbers (no division)

Given an array of numbers, nums, return an array of numbers products, where products[i] is the product of all nums[j], j != i.

Input : [1, 2, 3, 4, 5]
Output: [(2*3*4*5), (1*3*4*5), (1*2*4*5), (1*2*3*5), (1*2*3*4)]
= [120, 60, 40, 30, 24]
You must do this in O(N) without using division.

先建立一个array A,从右到左遍历,A[i]是这个东西右边所有东西的product。
然后从左边开始遍历,maintain在i位置左边的乘积, 乘上A[i]即可。

posted on 2015-03-31 23:09  Seth_L  阅读(88)  评论(0编辑  收藏  举报