Skip to content

Commit 4ee7e69

Browse files
authored
Merge pull request neetcode-gh#886 from therollingambit/238-product-of-array-except-self
adding golang solution for 238. product of array except self
2 parents 0db5850 + f682c2b commit 4ee7e69

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func productExceptSelf(nums []int) []int {
2+
res := make([]int, len(nums))
3+
for i := 0; i < len(res); i++ {
4+
res[i] = 1
5+
}
6+
7+
prefix := 1
8+
for i := 0; i < len(nums); i++ {
9+
res[i] = prefix
10+
prefix *= nums[i]
11+
}
12+
13+
postfix := 1
14+
for i := len(nums) - 1; i >= 0; i-- {
15+
res[i] *= postfix
16+
postfix *= nums[i]
17+
}
18+
19+
return res
20+
}

0 commit comments

Comments
 (0)