We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a1707ad commit 914d94bCopy full SHA for 914d94b
product-of-array-except-self/printjin-gmailcom.py
@@ -1,10 +1,13 @@
1
class Solution:
2
def productExceptSelf(self, nums):
3
- ans = []
4
- for i in range(len(nums)):
5
- product = 1
6
- for j in range(len(nums)):
7
- if j != i:
8
- product *= nums[j]
9
- ans.append(product)
10
- return ans
+ n = len(nums)
+ answer = [1] * n
+ left = 1
+ for i in range(n):
+ answer[i] = left
+ left *= nums[i]
+ right = 1
+ for i in range(n-1, -1, -1):
11
+ answer[i] *= right
12
+ right *= nums[i]
13
+ return answer
0 commit comments