两数之和

 

 

https://leetcode.cn/problems/two-sum/

 

func twoSum(nums []int, target int) []int {
    mp:=make(map[int]int,len(nums)) //某个数字出现的次数
    for i := range nums {
        if p, ok := mp[target-nums[i]]; ok {
            return []int{p, i}
        }
        mp[nums[i]] = i
    }
    return nil
}

 

posted @ 2022-06-21 17:58  知道了呀~  阅读(68)  评论(0编辑  收藏  举报