LeetCode001 - TwoSum

func twoSum(nums []int, target int) []int {
    solutions := []int{0, 0}
    numMap := make(map[int]int)

    for idx := 0; idx < len(nums); idx++ {
        numMap[nums[idx]] = idx
    }

    for idx1 := 0; idx1 < len(nums); idx1++ {
        idx2, isExist := numMap[target-nums[idx1]]
        if isExist && idx2 != idx1 {
            solutions[0] = idx1
            solutions[1] = idx2
            break
        }
    }
    return solutions
}

你可能感兴趣的:(LeetCode001 - TwoSum)