Skip to content

Commit

Permalink
adding golang solution for 238. product of array except self
Browse files Browse the repository at this point in the history
  • Loading branch information
therollingambit committed Aug 22, 2022
1 parent 2546faf commit f682c2b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions go/238-Product-Of-Array-Except-Self.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
func productExceptSelf(nums []int) []int {
res := make([]int, len(nums))
for i := 0; i < len(res); i++ {
res[i] = 1
}

prefix := 1
for i := 0; i < len(nums); i++ {
res[i] = prefix
prefix *= nums[i]
}

postfix := 1
for i := len(nums) - 1; i >= 0; i-- {
res[i] *= postfix
postfix *= nums[i]
}

return res
}

0 comments on commit f682c2b

Please sign in to comment.