From 92a2c4c046ae897f4ab5310e2fe70a29db41301a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khoa=20Hu=E1=BB=B3nh=20=28Daniel=29?= Date: Fri, 15 Mar 2024 23:52:05 +0700 Subject: [PATCH] Create main.go --- solution/238/main.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 solution/238/main.go diff --git a/solution/238/main.go b/solution/238/main.go new file mode 100644 index 0000000..1215361 --- /dev/null +++ b/solution/238/main.go @@ -0,0 +1,30 @@ +package main + +func productExceptSelf(nums []int) []int { + n := len(nums) + ans := make([]int, n) + product := 1 + zeros := 0 + + for _, num := range nums { + if num == 0 { + zeros++ + continue + } + product *= num + } + + if zeros == 1 { + for i, num := range nums { + if num == 0 { + ans[i] = product + } + } + } else if zeros == 0 { + for i, num := range nums { + ans[i] = product / num + } + } + + return ans +}