class Solution:
def findDisappearedNumbers(self, nums):
for i in range(len(nums)):
index = abs(nums[i]) - 1
if nums[index] > 0:
nums[index] = -nums[index]
return [i + 1 for i in range(len(nums)) if nums[i] > 0]
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
for (int i = 0; i < nums.size(); ++i) {
int index = abs(nums[i]) - 1;
if (nums[index] > 0) {
nums[index] = -nums[index];
}
}
vector<int> result;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] > 0) result.push_back(i + 1);
}
return result;
}
};
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int index = Math.abs(nums[i]) - 1;
if (nums[index] > 0) {
nums[index] = -nums[index];
}
}
List<Integer> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) result.add(i + 1);
}
return result;
}
}
var findDisappearedNumbers = function(nums) {
for (let i = 0; i < nums.length; i++) {
let index = Math.abs(nums[i]) - 1;
if (nums[index] > 0) {
nums[index] = -nums[index];
}
}
let result = [];
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) result.push(i + 1);
}
return result;
};
You are given an array of integers nums
where nums
has length n
and contains numbers between 1
and n
(inclusive). Some numbers may appear more than once, while others may be missing. Your task is to find all numbers in the range 1
to n
that do not appear in nums
, and return them as a list.
nums
is between 1
and n
(inclusive).
At first glance, the problem seems to ask for a simple search: for each number from 1
to n
, check if it's in nums
. A brute-force approach would use nested loops, checking every number for existence, but this would be inefficient (O(n^2)). A better idea is to use a set or hash map to record which numbers are present, then output those missing. However, the problem restricts us from using extra space (besides the output), so we need to be clever.
The key insight is that since all numbers are in the range 1
to n
, we can use the input array itself to store information about which numbers we've seen, by marking elements in a way that doesn't require extra space. This leads us to an in-place marking strategy.
num
in nums
.num
, compute its corresponding index: index = abs(num) - 1
.nums[index]
as negative to indicate that the number index + 1
exists in the array.nums
again.i
, if nums[i]
is positive, it means the number i + 1
was never seen in the array.i + 1
to the result list.nums
to mark presence, so we do not need extra space.1
to n
.
Suppose nums = [4,3,2,7,8,2,3,1]
.
[5, 6]
.This problem demonstrates a clever use of in-place array manipulation to record which numbers are present without using extra space. By leveraging the properties of the input (numbers from 1 to n), we can mark seen numbers by negating their corresponding indices. This approach is both efficient and elegant, achieving O(n) time and O(1) extra space. The main insight is recognizing that the input array itself can be used as a data structure to store auxiliary information, avoiding the need for additional memory.