-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathproblem1.java
More file actions
30 lines (22 loc) · 1.03 KB
/
problem1.java
File metadata and controls
30 lines (22 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Time Complexity : O(n)
// Space Complexity : O(1) (output list not counted)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just need to use abs() while marking.
// Your code here along with comments explaining your approach in three sentences only
// Since values are from 1 to n, we use each value as an index and mark that index as visited by making it negative.
// After marking, any position that still has a positive number means that (index+1) never appeared in the array.
// Finally, we collect all such missing numbers into the answer list.
import java.util.*;
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
int idx = Math.abs(nums[i]) - 1;
if (nums[idx] > 0) nums[idx] = -nums[idx];
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) res.add(i + 1);
}
return res;
}
}