simple ruby with 2 loops


  • 0
    T
    def product_except_self(nums)
        left = []
        right = []
        n = nums.length
        (0...nums.length).each do |i|
            left[i] = i == 0 ? 1 : left[i - 1] * nums[i - 1]
            right[n - 1 - i] = i == 0 ? 1 : right[n - i] * nums[n - i]
        end
        p = []
        (0...nums.length).each do |i|
            p[i] = left[i] * right[i]
        end
        return p
    end
    

Log in to reply
 

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