美文网首页
leetcode: 两数之和

leetcode: 两数之和

作者: 简书帅气的昵称已被使用 | 来源:发表于2019-06-03 23:05 被阅读0次

题目:

作者:gpe3DBjDS1
链接:https://leetcode-cn.com/problems/two-sum/solution/liang-shu-zhi-he-by-gpe3dbjds1/
来源:力扣(LeetCode)

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]

解答:

  • 方法1
func twoSum(nums []int, target int) []int {
    result := make([]int, 2)
    
    for i := 0; i < len(nums); i++ {
        tmpValue := nums[i]
        tmpIndex := i
        
        for index, value := range nums {
            if tmpIndex == index || index < tmpIndex {
                continue
            }
        
            sum := tmpValue + value
            if sum == target {
                result[0] = tmpIndex
                result[1] = index
                return result
            } 
        } 
    }
    
    return result
}

缺点:耗时长
特点:时间换空间

  • 方法2
func twoSum(nums []int, target int) []int {
    result := make([]int, 0)
    
    for i := 0; i < len(nums) - 1; i++ {
        for j := i +1; j < len(nums); j++ {
            sum := nums[i] + nums[j]
            if sum == target {
                result = append(result, i, j)
                return result
            }
        }
    }
    
    return result
}

缺点:耗时长,相比方法1耗时要短
特点:时间换空间

  • 方法3
func twoSum(nums []int, target int) []int {
    tmp := make(map[int]int, len(nums))
    result := make([]int, 0)

    for index, value := range nums {
        tmp[value] = index
    }

    for index, value := range nums {
        diffValue := target - value
        if diffIndex, ok := tmp[diffValue]; ok {
            if diffIndex == index {
                continue
            }

            result = append(result, index, diffIndex)
            return result
        }
    }

    return result
}

缺点:内存消耗较方法1和方法2大
特点:执行时间缩短,空间换时间

  • 方法4
func twoSum(nums []int, target int) []int {
    result := make([]int, 0)

    tmp := make(map[int]int, len(nums))
    for index, value := range nums {
        diffValue := target - value
        if diffIndex, ok := tmp[diffValue]; ok {
            result = append(result, diffIndex, index)
            return result
        }

        tmp[value] = index
    }

    return result
}

缺点:内存消耗较方法1、2、3都大
特点:执行速度快,空间换时间

相关文章

  • 【LeetCode通关全记录】1. 两数之和

    【LeetCode通关全记录】1. 两数之和 题目地址:1. 两数之和[https://leetcode-cn.c...

  • 双指针

    两数之和 click here for leetcode detail[https://leetcode-cn.c...

  • 纯C手撕leetcode-基本数据结构-hash table

    Hash table纯C实现两数之和和Hashtable 三数之和https://leetcode-cn.com/...

  • [LeetCode] TwoSum两数之和

    [LeetCode] TwoSum两数之和 Given an array of integers, returni...

  • LeetCode 专题 :双指针

    LeetCode 第 167 题:两数之和 II - 输入有序数组 传送门:167. 两数之和 II - 输入有序...

  • 常见算法面试题

    LeetCode题目精选 两数之和链接:https://leetcode-cn.com/problems/two-...

  • 两数之和-leetcode

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 你可以假设每个输入只对应一种答案,且同样的元素不能被...

  • LeetCode | 两数之和

    基础不好,笔试代码题没做好,校招没offer,赶紧来刷题 [TOC] 两数之和 这里采用两种方法来做,比较性能。 ...

  • [LeetCode] 两数之和

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 nums=[2, 7, 11, 15], targe...

  • leetcode 两数之和

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的...

网友评论

      本文标题:leetcode: 两数之和

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