Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1437 from tonidezman/main
Browse files Browse the repository at this point in the history
improved Golang solution for 238 leetcode challenge
  • Loading branch information
Ahmad-A0 authored Nov 19, 2022
2 parents 146bdb4 + e0d0b36 commit b49a4e1
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions go/238-Product-Of-Array-Except-Self.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
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++ {
for i, num := range nums {
res[i] = prefix
prefix *= nums[i]
prefix *= num
}

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

return res
}
}

0 comments on commit b49a4e1

Please sign in to comment.