LC 442 Find All Duplicates in an Array(M)
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime?
难点:不能用多余存储,而且 时间复杂度是O(n)
解题思路
不能用多余存储,则必须考虑利用当前数组传递信息。另外 1 ≤ a[i] ≤ n 很关键,
解题方法是 把数值设为负数,但扫描时候用 它的绝对值来 忽略 之前的更改。
Last updated
Was this helpful?