LeetCode 1. 两数之和

简介: LeetCode 1. 两数之和

LeetCode 1. 两数之和


Table of Contents

 

一、中文版

二、英文版

三、My answer

四、解题报告


一、中文版

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


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


示例:


给定 nums = [2, 7, 11, 15], target = 9


因为 nums[0] + nums[1] = 2 + 7 = 9

所以返回 [0, 1]


二、英文版

Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


三、My answer

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:       
        # version 1: 两层 for 循环,大数据会超时
        # for i in range(len(nums)):
        #     for j in range(len(nums)):
        #         if i != j and (nums[i] + nums[j] == target):
        #             return [i,j]
        # version 2:借助 dictionary,遍历一次数组,边遍历边存入 dictionary
        # _dict = {}
        # for i in range(len(nums)):
        #     if (target - nums[i]) in _dict:
        #         return [i,_dict[target - nums[i]]]
        #     else:
        #         _dict[nums[i]] = i
        # version 3:使用 enumerate() 改进 version 2
        _dict = {}
        for i,num in enumerate(nums):
            if (target - num) in _dict:
                return [i,_dict[target - num]]
            else:
                _dict[num] = i


四、解题报告

version 2 的精髓是把谁存入 dictionary?是把遍历过的数 num 存入 dictionary,而不是需要配对的数(target - num)存入,因为遍历过的数(num)不会再回头重新遍历,之后有可能遇到的也只是需要配对的数(target - num),此时需要查的是 num 是否在dictionary,所以 dictionary 中存的是 num 和 下标。


两数之和是经典题目,此题有很多变种,比如要求返回求和等于 target 的两个数值,则可以改为如下:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        nums.sort()
        i = 0
        j = len(nums) - 1
        while i < j:
            if nums[i] + nums[j] == target:
                return [i,j]
            elif nums[i] + nums[j] < target:
                i += 1
            else:
                j -= 1


相关文章
|
1月前
|
算法
LeetCode刷题---167. 两数之和 II - 输入有序数组(双指针-对撞指针)
LeetCode刷题---167. 两数之和 II - 输入有序数组(双指针-对撞指针)
|
1月前
【Leetcode】两数之和,给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
【Leetcode】两数之和,给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
|
1天前
|
存储 算法 索引
力扣经典150题第四十三题:两数之和
力扣经典150题第四十三题:两数之和
5 1
|
1天前
|
算法 测试技术 程序员
力扣经典150题第二十七题:两数之和 II - 输入有序数组
力扣经典150题第二十七题:两数之和 II - 输入有序数组
6 1
|
4天前
力扣-两数之和
力扣-两数之和
6 1
|
20天前
|
算法 数据挖掘 Java
深入解析力扣167题:两数之和 II(双指针法详解及模拟面试问答)
深入解析力扣167题:两数之和 II(双指针法详解及模拟面试问答)
|
24天前
|
存储 算法 Java
【经典算法】LeetCode1:两数之和(Java/C/Python3实现含注释说明,Easy)
【经典算法】LeetCode1:两数之和(Java/C/Python3实现含注释说明,Easy)
14 1
|
16天前
【LeetCode刷题】两数之和、两数相加
【LeetCode刷题】两数之和、两数相加
|
19天前
|
存储 算法 大数据
深入解析力扣170题:两数之和 III - 数据结构设计(哈希表与双指针法详解及模拟面试问答)
深入解析力扣170题:两数之和 III - 数据结构设计(哈希表与双指针法详解及模拟面试问答)
|
20天前
|
存储 SQL 数据挖掘
LeetCode 第一题:两数之和 【1/1000 python】
LeetCode 第一题:两数之和 【1/1000 python】