【发布时间】:2017-09-14 14:06:28
【问题描述】:
我正在用 leetcode 解决一个问题
给定一个包含 n + 1 个整数的数组 nums,其中每个整数都介于 1 和 n(含)之间,证明至少存在一个重复数。假设只有一个重复数,在O(n)时间和O(1)空间复杂度内找到重复的数
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
xor=0
for num in nums:
newx=xor^(2**num)
if newx<xor:
return num
else:
xor=newx
我接受了解决方案,但我被告知它既不是 O(1) 空间也不是 O(n) 时间。
谁能帮我理解为什么?
【问题讨论】:
标签: python algorithm data-structures