Description
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].
Solution
使用Bi-pointer , left记录左侧所有数字的乘积,right记录右侧所有乘积,对于每个数,只需left*right了得到结果
思路难,写简单
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
right = left =1
res =[1]*len(nums)
for i in range(len(nums)):
res[i]*=right
res[len(nums)-1-i]*=left
right *= nums[i]
left *= nums[len(nums)-1-i]
return res
网友评论