美文网首页
[Med-Biptr] 238. Product of Arra

[Med-Biptr] 238. Product of Arra

作者: Mree111 | 来源:发表于2019-10-16 13:37 被阅读0次

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

相关文章

网友评论

      本文标题:[Med-Biptr] 238. Product of Arra

      本文链接:https://www.haomeiwen.com/subject/dioymctx.html