Skip to content

Instantly share code, notes, and snippets.

@alldroll
Created March 17, 2020 08:25
Show Gist options
  • Select an option

  • Save alldroll/cafe5badeebb9de9d045f214d0f40614 to your computer and use it in GitHub Desktop.

Select an option

Save alldroll/cafe5badeebb9de9d045f214d0f40614 to your computer and use it in GitHub Desktop.
// https://leetcode.com/problems/product-of-array-except-self
func productExceptSelf(nums []int) []int {
numsLen := len(nums)
product := make([]int, numsLen)
product[0] = 1
for i := 1; i < numsLen; i++ {
product[i] = product[i - 1] * nums[i - 1]
}
r := 1
for i := numsLen - 1; i >= 0; i-- {
product[i] = product[i] * r
r *= nums[i]
}
return product
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment