two-sum
This commit is contained in:
parent
cc79df656f
commit
8c338a768f
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
// Time: O(n)
|
||||||
|
// Space: O(n)
|
||||||
|
func twoSum(nums []int, target int) []int {
|
||||||
|
exists := make(map[int]int)
|
||||||
|
for i1, num := range nums {
|
||||||
|
if i2, ok := exists[target-num]; ok && i1 != i2 {
|
||||||
|
return []int{i1, i2}
|
||||||
|
}
|
||||||
|
exists[num] = i1
|
||||||
|
}
|
||||||
|
return []int{}
|
||||||
|
}
|
Loading…
Reference in New Issue