Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6f4e508

Browse files
authored
Update Product_of_Array_Except_Self.py
1 parent 5176d01 commit 6f4e508

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎Leetcode_30day_challenge/Product_of_Array_Except_Self.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,28 @@ def productExceptSelf(self, nums: List[int]) -> List[int]:
2020
ans[n-1] = pre[n-2]
2121

2222
return ans
23+
24+
# Solution - 2: Time: O(N) Time and O(1) Space
25+
class Solution:
26+
def productExceptSelf(self, nums: List[int]) -> List[int]:
27+
n = len(nums)
28+
c = nums.count(0)
29+
prod = 1
30+
ans = [0]*n
31+
zero_idx = None
32+
# Calculating the Product
33+
for i,v in enumerate(nums):
34+
if v != 0:
35+
prod *= v
36+
else:
37+
zero_idx = i
38+
39+
if c > 1:
40+
return ans
41+
elif c == 0:
42+
for i in range(n):
43+
ans[i] = prod//nums[i]
44+
else:
45+
ans[zero_idx] = prod
46+
47+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /