Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Explanation:
- This solution works by finding the total multiplication of all the numbers to the left of the current number. Then loop again to find the multiplication of all numbers to the right.
- If the given array is: [1,2,3,4]
- The array would be changed so that every position represents the multiplication of the numbers to the left of it.
- [1,2,3,4] --> [1,1,2,6]
- Now we do the same thing from right to left, thus every position represents the multiplication of the numbers from the right to left.
- [1,1,2,6] --> [24,12,8,6]
- """
- class Solution:
- def productExceptSelf(self, nums: List[int]) -> List[int]:
- output = [1]*len(nums)
- prod = 1
- for i in range(len(nums)):
- output[i] *= prod
- prod *= nums[i]
- prod = 1
- for i in range(len(nums)-1, -1, -1):
- output[i] *= prod
- prod *= nums[i]
- return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement