238 Product of Array except itself

This commit is contained in:
2024-08-04 21:02:39 -04:00
parent 2d2ccc70bd
commit 33af13cb50
@@ -0,0 +1,18 @@
class Solution:
def productExceptSelf(self, nums: list[int]) -> list[int]:
ans = [1] * len(nums)
curr = 1
for i in range(len(nums)):
ans[i] *= curr
curr *= nums[i]
curr = 1
for i in range(len(nums) - 1, -1, -1):
ans[i] *= curr
curr *= nums[i]
return ans
if __name__ == "__main__":
test = Solution()
print(test.productExceptSelf([-1,1,0,-3,3]))