ricardovaltierra

238. Product of Array Except Self

May 27th, 2020
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # @param {Integer[]} nums
  2. # @return {Integer[]}
  3. def product_except_self(nums)
  4.   result = []
  5.   buffer = 1
  6.   result[0] = buffer  
  7.  
  8.   (1...nums.size).each { |i| result[i] = nums[i - 1] * result[i - 1]}  
  9.  
  10.   (nums.size - 1).downto(0).each { |e| result[e] = result[e] * buffer; buffer = buffer * nums[e] }
  11.  
  12.   result  
  13. end
RAW Paste Data