other than the product itself leetcode 238. array (Python)

A given length n integer array nums, wherein n> 1, the output returns an array output, wherein the output [i] is equal to the product of the remaining element other than nums [i] nums in.

Example:

Input: [1,2,3,4]
Output: [24,12,8,6]
Note: Do not use a divider, and completed this title in (n) time complexity O.

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        ln =len(nums)
        left,right = [1]*ln,[1]*ln
        for i in range(1,ln):
            left[i] = left[i-1]*nums[i-1] #left = [1, 1, 2, 6]
            right[ln-i-1] = right[ln-i]*nums[ln-i] #right = [24, 12, 4, 1]
        res = [1]*ln
        for i in range(ln):
            res[i] = left[i]*right[i]
        return res

Reference: https: //blog.csdn.net/weixin_43399785/article/details/88190888 

Guess you like

Origin www.cnblogs.com/xiaotongtt/p/11317880.html