442. 数组中重复的数据
解法一:O(lgn)
解法二:O(n)
class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
res = []
for i in range(len(nums)):
index = abs(nums[i]) - 1 #数组元素值映射为下标
if nums[index] < 0: #第二次访问
res.append(index+1)
nums[index] = -nums[index] #每次置反
return res最后更新于