Skip to content

Commit f35afca

Browse files
refactor 645
1 parent 1ef0f80 commit f35afca

File tree

1 file changed

+23
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+23
-0
lines changed

src/main/java/com/fishercoder/solutions/_645.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
46

57
public class _645 {
68
public static class Solution1 {
@@ -22,4 +24,25 @@ public int[] findErrorNums(int[] nums) {
2224
return result;
2325
}
2426
}
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+
}
2548
}

0 commit comments

Comments
 (0)