File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed
solution/0448.Find All Numbers Disappeared in an Array Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments