[Python] Easy thinking solution


  • -8
    D
    class Solution:
    # @param {integer[]} nums
    # @return {integer[]}
    def productExceptSelf(self, nums):
    	t = 1
    	zero = 0
    	for i in nums:
    		if i : t = t*i
    		else : zero =zero+1
    	
    	# if one zero, every thing is zero except for zero itself
    	if zero is 1 : return [0 if i else t for i in nums ]
    	# if two or more zero, every thing is zero
    	if zero > 1 : return [0 for i in nums ]
    	# if no zero, do the regular calculation
    	return [t/i for i in nums ]

  • 6
    D

    The question says "Solve it without division", does the t/i considered as a division operation?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.