Skip to content

Commit 9c0ea54

Browse files
refactor 645
1 parent d119cdf commit 9c0ea54

File tree

1 file changed

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

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,24 @@ public int[] findErrorNums(int[] nums) {
4545
return new int[]{dup, dup2};
4646
}
4747
}
48+
49+
public static class Solution3 {
50+
public int[] findErrorNums(int[] nums) {
51+
int[] arr = new int[nums.length];
52+
Set<Integer> set = new HashSet<>();
53+
int[] result = new int[2];
54+
for (int i = 0; i < nums.length; i++) {
55+
arr[i] = i + 1;
56+
if (!set.add(nums[i])) {
57+
result[0] = nums[i];
58+
}
59+
}
60+
for (int i = 0; i < nums.length; i++) {
61+
if (!set.contains(i + 1)) {
62+
result[1] = i + 1;
63+
}
64+
}
65+
return result;
66+
}
67+
}
4868
}

0 commit comments

Comments
 (0)