238. Product of Array Except Self
Last updated
Last updated
Given an array nums
of n integers where n > 1, return an array output
such that output[i]
is equal to the product of all the elements of nums
except nums[i]
.
Example:
Note: Please solve it without division and in O(n).
產生兩種 list 分別是由右累乘到左以及左累乘到右 input => [a,b,c,d]
由右累乘 => [1,d,cd,cdb,cdba]
由左累乘 => [1,a,ab,abc,abcd]
將兩條 list 相乘 [cdb*1 ,cd*a ,d*ab ,1*abc]
初始化 output list 使其皆為 1
於第一個迴圈將由左至右累乘的數字存進 output list 中 其第一個值為 1 (預設)
於第二個迴圈將由左至右累乘的數字乘進原本的 output list 中