Skip to content

Commit

Permalink
update sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Nov 27, 2023
1 parent fa585c5 commit 6bcb66b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion leetcode/01.two_sum/two_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,24 @@ func twoSum(nums []int, target int) []int {
return results
}

func twoSum1(nums []int, target int) []int {
m := map[int]int{}

for i := range nums {
temp := nums[i]
search := target - temp

if j, ok := m[search]; ok {
return []int{i, j}
}

m[temp] = i
}
return []int{}
}

func main() {
nums := []int{2, 7, 11, 15}
target := 9
fmt.Println(twoSum(nums, target))
fmt.Println(twoSum1(nums, target))
}

0 comments on commit 6bcb66b

Please sign in to comment.