Leetcode实战:238. 除自身以外数组的乘积

题目:

给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。

示例:

1
2
输入: [1,2,3,4]
输出: [24,12,8,6]

算法实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
a = []
temp = 1
for i in nums:
a.append(temp)
temp *= i
nums.reverse()
temp = 1
count = len(nums) - 1
for j in nums:
a[count] *= temp
temp *= j
count -= 1
return a

结果:

在这里插入图片描述

Leetcode实战:238. 除自身以外数组的乘积
http://chenxindaaa.com/Programming/Leetcode/Leetcode/Leetcode实战:238. 除自身以外数组的乘积/
Author
chenxindaaa
Posted on
October 8, 2019
Licensed under