File tree Expand file tree Collapse file tree 3 files changed +18
-9
lines changed
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree 3 files changed +18
-9
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,9 @@ public List<Integer> findDuplicates(int[] nums) {
13
13
List <Integer > res = new ArrayList <>();
14
14
for (int i = 0 ; i < nums .length ; ++i ) {
15
15
int index = Math .abs (nums [i ]) - 1 ;
16
- if (nums [index ] < 0 )
16
+ if (nums [index ] < 0 ) {
17
17
res .add (Math .abs (index + 1 ));
18
+ }
18
19
nums [index ] = -nums [index ];
19
20
}
20
21
return res ;
Original file line number Diff line number Diff line change 8
8
*/
9
9
10
10
public class FindAllNumbersDisappearedInAnArray {
11
+ //4,3,2,7,8,2,3,1
11
12
13
+ /**
14
+ * 这题的前提是数组的size为n,元素范围为[1,n]
15
+ * 核心思想是给所有出现过的数标为负
16
+ * 那剩下的为正的数就是没出现过的
17
+ */
12
18
public List <Integer > findDisappearedNumbers (int [] nums ) {
13
19
List <Integer > ret = new ArrayList <Integer >();
14
20
15
- for (int i = 0 ; i < nums .length ; i ++) {
21
+ for (int i = 0 ; i < nums .length ; i ++) {
16
22
int val = Math .abs (nums [i ]) - 1 ;
17
- if (nums [val ] > 0 ) {
23
+ if (nums [val ] > 0 ) {
18
24
nums [val ] = -nums [val ];
19
25
}
20
26
}
21
27
22
- for (int i = 0 ; i < nums .length ; i ++) {
23
- if (nums [i ] > 0 ) {
24
- ret .add (i + 1 );
28
+ for (int i = 0 ; i < nums .length ; i ++) {
29
+ if (nums [i ] > 0 ) {
30
+ ret .add (i + 1 );
25
31
}
26
32
}
27
33
return ret ;
Original file line number Diff line number Diff line change 1
1
package com .inuker .test ;
2
2
3
+ import com .inuker .solution .FindAllNumbersDisappearedInAnArray ;
3
4
import com .inuker .solution .MinimumSizeSubarraySum ;
4
5
import com .inuker .solution .NextPermutation ;
5
6
import com .inuker .solution .SearchForARange ;
@@ -31,9 +32,10 @@ public class main {
31
32
32
33
public static void main (String [] args ) {
33
34
int [] arr = new int []{
34
- 1 , 2 , 3 , 4 , 5
35
+ 4 , 3 , 2 , 7 , 8 , 2 , 3 , 1
35
36
};
36
- int len = new MinimumSizeSubarraySum ().minSubArrayLen2 (15 , arr );
37
- System .out .println (len );
37
+ for (int n : new FindAllNumbersDisappearedInAnArray ().findDisappearedNumbers (arr )) {
38
+ System .out .print (n + " " );
39
+ }
38
40
}
39
41
}
You can’t perform that action at this time.
0 commit comments