以下使用字典的python代码的时间复杂度是多少?

时间:2018-08-10 05:00:46

标签: python performance dictionary time-complexity

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),但是我认为“如果compliment_of_num in compliments”行是O(n)而不是O(1),这会使整个程序的复杂度变为O(N ^ 2)

0 个答案:

没有答案