Simple Python solution in O(n) using RE


  • 0
    C

    '''
    class Solution(object):
    def productExceptSelf(self, nums):
    """
    :type nums: List[int]
    :rtype: List[int]
    """
    import re
    e = re.compile(r'0')
    if len(e.findall(''.join(list(map(str,nums))))) > 1:
    return list(map(int,len(nums) * '0'))
    result = []
    total = reduce(lambda x,y:xy,nums)
    for i in range(len(nums)):
    if nums[i] == 0:
    nums[i] = 1
    result.append(reduce(lambda x,y:x
    y,nums))
    else:
    result.append(total/nums[i])
    return result
    '''


Log in to reply
 

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