1. Two Sum.go

作者: AnakinSun | 来源:发表于2019-03-22 02:33 被阅读1次

用hash保存另一个数的值,遍历的同时判断是否存在

func twoSum(nums []int, target int) []int {
    m := make(map[int]int)
    for i := 0; i < len(nums); i++ {
        c, ok := m[nums[i]]
        if ok {
            r := []int{c, i}
            return r
        } else {
            m[target-nums[i]] = i
        }
    }
    r := []int{0, 0}
    return r
}

相关文章

  • 1. Two Sum.go

    用hash保存另一个数的值,遍历的同时判断是否存在

  • 1. Two Sum

  • 1. Two Sum

    Given an array of integers, return indices of the two num...

  • 1. Two Sum

    Description Given an array of integers, return indices of...

  • 1. Two Sum

    Problem Given an array of integers, return indices of the...

  • 1. Two Sum

    Given an array of integers, return indices of the two num...

  • 1. Two Sum

    Leetcode: 1. Two SumGiven an array of integers, return in...

  • 1. Two Sum

    Example:Given nums = [2, 7, 11, 15], target = 9,Because n...

  • 1. Two Sum

    描述 Given an array of integers, return indices of the two ...

  • 1. Two Sum

    Description Given an array of integers, return indices of...

网友评论

    本文标题:1. Two Sum.go

    本文链接:https://www.haomeiwen.com/subject/meskvqtx.html