算法之美_计算整数数组中两个整数之和等于目标值的下标

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

算法复杂度O(n)

实现方式:遍历数据,使用hash表字典表保存遍历过的数字及下标,再从hash字典表中查找出等于当前遍历数字的计算组合数字。

def twoSum(nums: List[int], target: int) -> List[int]:

        length = len(nums);

        hashmap = {}

        for i in range(length):

            value = target - nums[i];

            if(value in hashmap):

                return [hashmap[value], i]

            hashmap[nums[i]] = i

            i+=1;

        return None;

你可能感兴趣的:(算法之美_计算整数数组中两个整数之和等于目标值的下标)