首页 > 解决方案 > 以下使用字典的python代码的时间复杂度是多少?

问题描述

def twoSum(self, nums, target):

    compliments = {}
    length = len(nums)

    for i, num in enumerate(nums):
        compliment_of_num = target-num
        if compliment_of_num in compliments:
            return [nums.index(compliment_of_num),i]
        compliments.update({num:compliment_of_num})

我认为它的 O(n) 但我认为“如果恭维中的恭维_of_num”这一行是 O(n) 而不是 O(1) 这使得整个程序的复杂性为 O(N^2)

标签: pythonperformancedictionarytime-complexity

解决方案


推荐阅读