python - 以下使用字典的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)
解决方案
推荐阅读
- flutter - 在 Flutter 中延迟加载图像?
- android - [Android][ListView]片段中无法显示ListView
- javascript - Javascript从和对象中找到具有最小(或最大)值的n个键
- javascript - 在 Phaser 3 中加载场景插件
- javascript - 如何为我的侧边栏中的每个部分制作不同的应用栏?[反应Js]
- python - 如何在 Python 中修改绘图的值
- intellij-idea - 如何在 Kotlin 中激活类型注释提示,如图所示?
- visual-studio - 我可以将 Visual Studio Code 直接连接到服务器吗?
- odata - sapui5 odata 在提交更改错误后恢复模型
- html - 使用带有弹性容器的 bootstrap 4 设计一些设计