diff --git a/product-of-array-except-self/sol.go b/product-of-array-except-self/sol.go new file mode 100644 index 0000000..84ddff2 --- /dev/null +++ b/product-of-array-except-self/sol.go @@ -0,0 +1,18 @@ +package main + +// Time: O(n) +// Space: O(1) +func productExceptSelf(nums []int) []int { + l := len(nums) + answer := make([]int, l) + answer[0] = 1 + for idx := 1; idx < l; idx++ { + answer[idx] = answer[idx-1] * nums[idx-1] + } + m := 1 + for idx := l - 1; idx >= 0; idx-- { + answer[idx] *= m + m *= nums[idx] + } + return answer +}