feat(1-two-sum): add golang best soln
This commit is contained in:
parent
6e6014bcc8
commit
54afd0abcc
14
0001_two-sum/golang/two-sum-best-approach.go
Normal file
14
0001_two-sum/golang/two-sum-best-approach.go
Normal file
@ -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{}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user