66import java .util .Map ;
77
88/**
9+ * 448. Find All Numbers Disappeared in an Array
10+ *
911 * Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
10-
11- Find all the elements of [1, n] inclusive that do not appear in this array.
12-
13- Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
12+ * Find all the elements of [1, n] inclusive that do not appear in this array.
13+ * Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
1414
1515 Example:
1616
@@ -19,75 +19,68 @@ Could you do it without extra space and in O(n) runtime? You may assume the retu
1919
2020 Output:
2121 [5,6]
22+
2223 */
2324public class _448 {
2425
25- /**O(n) space
26- * O(n) time*/
27- public List <Integer > findDisappearedNumbers_1 (int [] nums ) {
26+ public static class Solution1 {
27+ /**
28+ * O(n) space
29+ * O(n) time
30+ */
31+ public List <Integer > findDisappearedNumbers (int [] nums ) {
2832
29- int max = Integer .MIN_VALUE ;
30- for (int i : nums ) {
31- max = Math .max (max , i );
32- }
33- max = Math .max (max , nums .length );
34- //if using extra space is allowed, it'll be super easy as follows:
35- Map <Integer , Integer > map = new HashMap ();
36- for (int i = 1 ; i <= max ; i ++) {
37- map .put (i , 0 );
38- }
39-
40- for (int i : nums ) {
41- if (map .get (i ) == 0 ) {
42- map .put (i , 1 );
43- } else {
44- map .put (i , map .get (i ) + 1 );
33+ int max = Integer .MIN_VALUE ;
34+ for (int i : nums ) {
35+ max = Math .max (max , i );
4536 }
46- }
37+ max = Math . max ( max , nums . length );
4738
48- List <Integer > result = new ArrayList ();
49- for (int i : map .keySet ()) {
50- if (map .get (i ) == 0 ) {
51- result .add (i );
39+ Map <Integer , Integer > map = new HashMap ();
40+ for (int i = 1 ; i <= max ; i ++) {
41+ map .put (i , 0 );
5242 }
53- }
54-
55- return result ;
5643
57- }
58-
59- /**
60- * O(1) space
61- * O(n) time
62- */
63- public List <Integer > findDisappearedNumbers_2 (int [] nums ) {
64-
65- for (int i = 0 ; i < nums .length ; i ++) {
66- int val = Math .abs (nums [i ]) - 1 ;
67- if (nums [val ] > 0 ) {
68- nums [val ] = -nums [val ];
44+ for (int i : nums ) {
45+ if (map .get (i ) == 0 ) {
46+ map .put (i , 1 );
47+ } else {
48+ map .put (i , map .get (i ) + 1 );
49+ }
6950 }
70- }
7151
72- List <Integer > result = new ArrayList ();
73- for (int i = 0 ; i < nums .length ; i ++) {
74- if (nums [i ] > 0 ) {
75- result .add (i + 1 );
52+ List <Integer > result = new ArrayList ();
53+ for (int i : map .keySet ()) {
54+ if (map .get (i ) == 0 ) {
55+ result .add (i );
56+ }
7657 }
58+ return result ;
7759 }
78-
79- return result ;
80-
8160 }
8261
83- public static void main (String ... args ) {
84- _448 test = new _448 ();
85- // int[] nums = new int[]{4,3,2,7,8,2,3,1};
86- int [] nums = new int []{1 , 1 };
87- List <Integer > result = test .findDisappearedNumbers_2 (nums );
88- for (int i : result ) {
89- System .out .println (i );
62+ public static class Solution2 {
63+ /**
64+ * O(1) space
65+ * O(n) time
66+ */
67+ public List <Integer > findDisappearedNumbers_2 (int [] nums ) {
68+
69+ for (int i = 0 ; i < nums .length ; i ++) {
70+ int val = Math .abs (nums [i ]) - 1 ;
71+ if (nums [val ] > 0 ) {
72+ nums [val ] = -nums [val ];
73+ }
74+ }
75+
76+ List <Integer > result = new ArrayList ();
77+ for (int i = 0 ; i < nums .length ; i ++) {
78+ if (nums [i ] > 0 ) {
79+ result .add (i + 1 );
80+ }
81+ }
82+ return result ;
9083 }
9184 }
9285
93- }
86+ }
0 commit comments