Skip to content

Commit 5b703c5

Browse files
committed
Add Solution.java for 0448.Find All Numbers Disappeared in an Array
1 parent 2b040fc commit 5b703c5

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Find All Numbers Disappeared in an Array
2+
3+
Given an array of integers where **1 ≤ a[i] ≤ n** (n = _size of array_), some elements appear twice and others appear once.
4+
5+
Find all the elements of [1, _n_] inclusive that do not appear in this array.
6+
7+
Could you do it without extra space and in O(_n_) runtime? You may assume the returned list does not count as extra space.
8+
9+
## Example:
10+
```
11+
Input: [4,3,2,7,8,2,3,1]
12+
Output: [5,6]
13+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public List<Integer> findDisappearedNumbers(int[] nums) {
3+
List<Integer> result = new ArrayList<>();
4+
boolean[] inx = new boolean[nums.length + 1];
5+
for (int num : nums) {
6+
inx[num] = true;
7+
}
8+
for (int i = 1, length = nums.length; i <= length; i++) {
9+
if (!inx[i]) {
10+
result.add(i);
11+
}
12+
}
13+
return result;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public List<Integer> findDisappearedNumbers(int[] nums) {
3+
List<Integer> result = new ArrayList<>();
4+
for (int i = 0, length = nums.length; i < length; i++) {
5+
int abs = Math.abs(nums[i]);
6+
nums[abs - 1] = -Math.abs(nums[abs - 1]);
7+
}
8+
for (int i = 0, length = nums.length; i < length; i++) {
9+
if (nums[i] > 0) {
10+
result.add(i+1);
11+
}
12+
}
13+
return result;
14+
}
15+
}

0 commit comments

Comments
 (0)