- 18 - Hard - Product of Array Exc
- array_product()—返回迭代计算数组键值的乘积;
- LeetCode 238. Product of Array E
- 238. Product of Array Except Sel
- Leetcode解题笔记-238. Product of Arr
- Leetcode 【238、1011】
- Maximum Product of Three Numbers
- LeetCode-628. Maximum Product of
- 628. Maximum Product of Three Nu
- 628. Maximum Product of Three Nu
一个长度为 n 的整形数组nums,其中 n > 1,返回一个数组 output ,其中 output[i] 等于nums中除nums[i]以外所有元素的乘积。
不用除法 且在O(n)内解决这个问题。
例如,输入 [1,2,3,4],返回 [24,12,8,6]。
进阶:
你可以在常数空间复杂度内解决这个问题吗?(注意:出于空间复杂度分析的目的,输出数组不被视为额外空间。)
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res,p=[],1
for i in nums:
res.append(p)
p*=i
p=1
for i in range(len(nums)-1,-1,-1):
res[i]*=p
p*=nums[i]
return res
网友评论