File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
import java .util .Arrays ;
4
+ import java .util .HashSet ;
5
+ import java .util .Set ;
4
6
5
7
public class _645 {
6
8
public static class Solution1 {
@@ -22,4 +24,25 @@ public int[] findErrorNums(int[] nums) {
22
24
return result ;
23
25
}
24
26
}
27
+
28
+ public static class Solution2 {
29
+ public int [] findErrorNums (int [] nums ) {
30
+ Set <Integer > set = new HashSet ();
31
+ boolean [] has = new boolean [nums .length ];
32
+ int dup = -1 ;
33
+ for (int i = 0 ; i < nums .length ; i ++) {
34
+ has [nums [i ] - 1 ] = true ;
35
+ if (!set .add (nums [i ])) {
36
+ dup = nums [i ];
37
+ }
38
+ }
39
+ int dup2 = -1 ;
40
+ for (int i = 0 ; i < has .length ; i ++) {
41
+ if (!has [i ]) {
42
+ dup2 = i + 1 ;
43
+ }
44
+ }
45
+ return new int []{dup , dup2 };
46
+ }
47
+ }
25
48
}
You can’t perform that action at this time.
0 commit comments