use range-for

range for is much easy to read
This commit is contained in:
JessonChan
2020-08-13 19:06:25 +08:00
committed by GitHub
parent 000a261bd2
commit 7fd680c014

View File

@ -2,12 +2,11 @@ package leetcode
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i := 0; i < len(nums); i++ {
another := target - nums[i]
if _, ok := m[another]; ok {
return []int{m[another], i}
for k, v := range nums {
if idx, ok := m[target-v]; ok {
return []int{idx, k}
}
m[nums[i]] = i
m[v] = k
}
return nil
}