king_crimson10

Product except self

May 5th, 2022
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1.     def productExceptSelf(nums):
  2.         zero_cnt = 0
  3.         mul = 1
  4.         zero_flag = False
  5.        
  6.         for num in nums:
  7.             if num != 0:
  8.                 mul *= num
  9.             else:
  10.                 zero_cnt+=1
  11.                 zero_flag = True
  12.                 if zero_cnt>1:
  13.                     return [0] * len(nums)
  14.  
  15.        
  16.         res = []
  17.        
  18.         if zero_flag==True:
  19.             for num in nums:
  20.                 if num==0:
  21.                     res.append(mul)
  22.                 else:
  23.                     res.append(0)
  24.         else:
  25.             for num in nums:
  26.                 res.append(int(mul/num))
  27.         return res
Advertisement
Add Comment
Please, Sign In to add comment