Skip to content

Commit 13bdec4

Browse files
committed
Product Except Self
1 parent b66fa9f commit 13bdec4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎238. Product of Array Except Self.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def productExceptSelf(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[int]
6+
"""
7+
8+
product = 1
9+
flag = False
10+
count = 0
11+
for i in nums:
12+
if(i==0):
13+
flag = True
14+
count+=1
15+
if(i!=0):
16+
product *= i
17+
arr = []
18+
19+
for i in range(len(nums)):
20+
if(flag):
21+
if(nums[i]!=0 or count>1):
22+
arr.append(0)
23+
elif(nums[i]==0 and count==1):
24+
arr.append(product)
25+
else:
26+
arr.append(product/nums[i])
27+
return arr
28+
29+
30+
31+

0 commit comments

Comments
 (0)