From 54afd0abccd2e24d3352b32d4f0dc5f75cb84eb4 Mon Sep 17 00:00:00 2001 From: Sangeeth Sudheer Date: Sat, 1 Jan 2022 22:24:04 +0530 Subject: [PATCH] feat(1-two-sum): add golang best soln --- 0001_two-sum/golang/two-sum-best-approach.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 0001_two-sum/golang/two-sum-best-approach.go diff --git a/0001_two-sum/golang/two-sum-best-approach.go b/0001_two-sum/golang/two-sum-best-approach.go new file mode 100644 index 0000000..8aeafc6 --- /dev/null +++ b/0001_two-sum/golang/two-sum-best-approach.go @@ -0,0 +1,14 @@ +func twoSum(nums []int, target int) []int { + // Optimal solution: using a hashtable + m := make(map[int]int) + + for i, num := range nums { + if j, ok := m[target-num]; ok { + return []int{i, j} + } + + m[num] = i + } + + return []int{} +}