Skip to content

Instantly share code, notes, and snippets.

@theabbie
Created May 9, 2022 09:36
Show Gist options
  • Save theabbie/b1911c77524a18da6929bdd22455c8e3 to your computer and use it in GitHub Desktop.
Save theabbie/b1911c77524a18da6929bdd22455c8e3 to your computer and use it in GitHub Desktop.
Product of array except self
class Solution:
def product_exclude_itself(self, nums):
n = len(nums)
l = [1]
r = [1]
for i in range(n):
l.append(l[-1] * nums[i])
r.append(r[-1] * nums[n - i - 1])
return [l[i] * r[n - i - 1] for i in range(n)]
Solution().product_exclude_itself([1, 2, 3]) # [2*3, 1*3, 1*2] => [6, 3, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment