- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我上网查了一下才知道list.pop()
时间复杂度为 O(1) 但 list.pop(i)
有 O(n) 的时间复杂度。我在写 leetcode 的时候,很多人都在用 pop(i)
在 for 循环中,他们说它是 O(n) 时间复杂度,实际上它比我的代码快,我的代码只使用一个循环,但在该循环中使用多行。我想知道为什么会发生这种情况,我应该使用 pop(i)
而不是很多行来避免它?
示例:Leetcode 26. 从排序数组中删除重复项
我的代码:(比 75% 快)
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, 0
count = 1
while right < len(nums)-1:
if nums[right] == nums[right+1]:
right += 1
else:
nums[left+1]=nums[right+1]
left += 1
right += 1
count += 1
return count
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, 0
while right < len(nums)-1:
if nums[right] != nums[right+1]:
nums[left+1]=nums[right+1]
left += 1
right += 1
return left + 1
最佳答案
您的算法确实需要 O(n) 时间,而“逆序弹出”算法确实需要 O(n²) 时间。然而,LeetCode 并未报告您的时间复杂度优于 89% 的提交;它报告您的实际运行时间优于所有提交的 89%。实际运行时间取决于测试算法的输入;不仅是大小,还有重复的数量。
它还取决于如何平均跨多个测试用例的运行时间;如果大多数测试用例是针对二次解更快的小输入,那么即使二次解的时间复杂度更高,它也可能整体领先。 @Heap Overflow 在评论中还指出,LeetCode 判断系统的开销时间与算法运行所需的时间相比成比例地大且变化很大,因此差异可能仅仅是由于该开销的随机变化造成的。
为了阐明这一点,我使用 timeit 测量了运行时间。 .下图显示了我的结果;考虑到时间复杂性,这些形状正是您所期望的,并且交叉点介于 8000 < n < 9000
之间。在我的机器上。这是基于排序列表,其中每个不同的元素平均出现两次。我用来生成时间的代码如下。
计时码:
def linear_solution(nums):
left, right = 0, 0
while right < len(nums)-1:
if nums[right] != nums[right+1]:
nums[left+1]=nums[right+1]
left += 1
right += 1
return left + 1
def quadratic_solution(nums):
prev_obj = []
for i in range(len(nums)-1,-1,-1):
if prev_obj == nums[i]:
nums.pop(i)
prev_obj = nums[i]
return len(nums)
from random import randint
from timeit import timeit
def gen_list(n):
max_n = n // 2
return sorted(randint(0, max_n) for i in range(n))
# I used a step size of 1000 up to 15000, then a step size of 5000 up to 50000
step = 1000
max_n = 15000
reps = 100
print('n', 'linear time (ms)', 'quadratic time (ms)', sep='\t')
for n in range(step, max_n+1, step):
# generate input lists
lsts1 = [ gen_list(n) for i in range(reps) ]
# copy the lists by value, since the algorithms will mutate them
lsts2 = [ list(g) for g in lsts1 ]
# use iterators to supply the input lists one-by-one to timeit
iter1 = iter(lsts1)
iter2 = iter(lsts2)
t1 = timeit(lambda: linear_solution(next(iter1)), number=reps)
t2 = timeit(lambda: quadratic_solution(next(iter2)), number=reps)
# timeit reports the total time in seconds across all reps
print(n, 1000*t1/reps, 1000*t2/reps, sep='\t')
关于Python list.pop(i) 时间复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59744621/
所以下面的内容让我很困惑。 #!/usr/bin/python test = [0, 0, 0, 1, 2, 3, 4, 5, 6] test1 = [0, 0, 0, 1, 2, 3, 4, 5,
这个问题是这个问题的后续问题: deque.popleft() and list.pop(0). Is there performance difference? 在 Python 中,我可以使用 .
我正在使用 bootstrap v2.2.2。我尝试了其他一些方法(即: close popover outside popover but inside stay open 和 How to dis
我正在用 Python 创建提交后脚本并使用子进程调用 git 命令。 在我的脚本中,我想在运行某些命令之前存储所有更改,然后将它们 pop 。问题是,如果没有任何东西可以存储,stash pop 会
我有一个嵌入在 UINavigationController 中的 UITableViewController,我正在尝试将 Peek & Pop 实现到 TableView 中。我的“窥视”部分工作
我的 Windows 机器上安装了 Cygwin、msysgit 和 TortoiseGit。我正在为 Cygwin 编写一个脚本,该脚本通过 ssh 将 git 推送到远程机器: git push
我在 Jenkins 中使用groovy,并且我需要这个字符串来获取其中的最后一个单词。假设字符串是 STATUS = "EXECUTE SIT" 。所以我所做的就是分割字符串,这样我就会得到一个数组
本文是不太具体的问题的后续/重新表述 Is it possible to have a hyperlink inside {content:"..."}? . 用户 Naeem Shaikh ,非常感
Navigator.of(context).pop 和 Navigator.pop(context) 有什么区别? 对我来说两者似乎都在做同样的工作,实际的区别是什么。一个被弃用了吗? 最佳答案 Na
这可能吗?我想要一个更简单的命令来 git stash pop stash@{13} 其中 stash@{13} 只是 last 意思是“最后的存储在列表上”或“最古老的藏品”。 我知道我可以为 gi
Closed. This question is not reproducible or was caused by typos。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-to
Visual Studio 2019 中用于 GIT 存储的以下命令有什么区别? 分阶段 pop 和恢复 (--index) 全部 pop 为未暂存状态 使用https://visualstudio.
我想弹出模型的最后一层。所以我使用了 tf.keras.layers.pop(),但它不起作用。 base_model.summary() base_model.layers.pop() base_m
我想使用 navigator.pop 将值从第 2 页传递到第 1 页,并使用 initstate 中的新值刷新或重新加载我的第 1 页或任何其他解决方法? 我能够在第一页中获取这些值,但无法使用 i
pop 函数的文档说: user> (doc pop) ------------------------- clojure.core/pop ([coll]) For a list or queu
我有以下点击处理程序,当点击它时,我从 handsontable 中提取一个数组然后从数组中删除最后一个元素,并将新数组传递给 ajax post。问题是,如果我再次单击该按钮,它将从数组中删除另一个
我在mailmuch中制作了表单并从中获取了代码,我添加到网页并使用href,当用户单击显示弹出窗口时显示表单。没关系 show popup 但是现在我有ajax请求,我希望在ajax返回成功时显示此
我目前正在学习 Python 中的 pop() 函数并有一个问题。 >>> a = [1,2,3,4] >>> a.pop(3) #or a.pop() 4 >>> print(a) [1,2,3]
我目前正在学习 Python 中的 pop() 函数并有一个问题。 >>> a = [1,2,3,4] >>> a.pop(3) #or a.pop() 4 >>> print(a) [1,2,3]
我可以将对象$push编码到Mongo数组上,如下所示: db.foo.update({},{$push:{bar:3}}) 但是我找不到一种语法,可以让我对列表中的最后一项进行$pop编码。 我已经
我是一名优秀的程序员,十分优秀!