Find All Numbers Disappeared in an Array LeetCode solution in Java

1
class Solution {
2
public List<Integer> findDisappearedNumbers(int[] nums) {
3
List<Integer> res = new ArrayList<>();
4
// 0 1 2 3 4 5 6 7 <- indx
5
// 4 3 2 7 8 2 3 1 <- nums[i]
6
for (int i = 0; i < nums.length; i++) {
7
int indx = Math.abs(nums[i]) - 1;
8
if (nums[indx] > 0) {
9
nums[indx] = nums[indx] * -1;
10
}
11
}
12
// 0 1 2 3 4 5 6 7 <- indx
13
// -4 -3 -2 -7 8 2 -3 -1 <- nums[i]
14
for (int i = 0; i < nums.length; i++) {
15
if (nums[i] > 0) {
16
res.add(i + 1);
17
} else {
18
nums[i] *= -1;
19
}
20
}
21
// [ 5, 6]
22
return res;
23
}
24
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0