Created
May 9, 2022 09:36
-
-
Save theabbie/b1911c77524a18da6929bdd22455c8e3 to your computer and use it in GitHub Desktop.
Product of array except self
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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