Skip to content

Commit dba2c71

Browse files
authored
Merge pull request cheehwatang#203 from cheehwatang/update-645-SetMismatch
Update 645. Set Mismatch (Sorting)
2 parents 90e3246 + 3bd20f2 commit dba2c71

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@
2727
<th>Solution</th>
2828
<th>Topics</th>
2929
</tr>
30+
<tr>
31+
<td align="center">May 8th</td>
32+
<td>645. <a href="https://leetcode.com/problems/set-mismatch/">Set Mismatch</a></td>
33+
<td align="center">$\text{\color{TealBlue}Easy}$</td>
34+
<td align="center">
35+
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/645.%20Set%20Mismatch/SetMismatch_Sorting.java">Sorting</a>
36+
</td>
37+
<td align="center">
38+
<a href="#array">Array</a>,
39+
<a href="#sorting">Sorting</a>
40+
</td>
41+
</tr>
3042
<tr>
3143
<td align="center">May 7th</td>
3244
<td>645. <a href="https://leetcode.com/problems/set-mismatch/">Set Mismatch</a></td>
@@ -76,18 +88,6 @@
7688
<a href="#string">String</a>
7789
</td>
7890
</tr>
79-
<tr>
80-
<td align="center">May 3rd</td>
81-
<td>557. <a href="https://leetcode.com/problems/reverse-words-in-a-string-iii/">Reverse Words in a String III</a></td>
82-
<td align="center">$\text{\color{TealBlue}Easy}$</td>
83-
<td align="center">
84-
<a href="https://github.com/cheehwatang/leetcode-java/blob/main/solutions/557.%20Reverse%20Words%20in%20a%20String%20III/ReverseWordsInAStringIII_TwoPointers.java">Two Pointers</a>
85-
</td>
86-
<td align="center">
87-
<a href="#string">String</a>,
88-
<a href="#two-pointers">Two Pointers</a>
89-
</td>
90-
</tr>
9191
</table>
9292
</br>
9393
<hr>

solutions/645. Set Mismatch/SetMismatch_Sorting.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
package com.cheehwatang.leetcode;
22

3-
/**
4-
* Problem:
5-
* An array of integers 'nums' with length of 'n', which originally contains all the numbers from 1 to 'n',
6-
* but currently contains an error, which resulted in the repetition of one number and loss of another number.
7-
* Return an integer array representing:
8-
* 1. the number that occurs twice, and
9-
* 2. the number that is missing.
10-
*
11-
*
12-
* Example 1:
13-
* Input : nums = [1,1]
14-
* Output : [1,2]
15-
* Explanation: The integer '1' occurs twice, while integer '2' is missing.
16-
*
17-
*
18-
* Example 2:
19-
* Input : nums = [3,2,2,4]
20-
* Output : [2,1]
21-
* Explanation: The 'nums' contains the number from 1 to 4. The integer '2' occurs twice, while integer '1' is missing.
22-
*
23-
*
24-
* @author Chee Hwa Tang
25-
*/
3+
// Time Complexity : O(n),
4+
// where 'n' is the length of 'nums'.
5+
// Since we know that the sorted array would be from 1 to 'n', the implemented sorting function only has
6+
// time complexity of O(2n), with the sorting and traversal of 'nums' each results in O(n).
7+
// Additionally, we traverse the array once more to find the missing and the duplicate numbers.
8+
//
9+
// Space Complexity : O(1),
10+
// as the auxiliary space used is independent on the size of the input.
2611

2712
public class SetMismatch_Sorting {
2813

@@ -44,26 +29,30 @@ public int[] findErrorNums(int[] nums) {
4429
int correct = nums[i] - 1;
4530
// If the integer at 'i' is not at the correct index, we swap nums[i] to its correct position.
4631
// As the number swapped to position 'i' maybe not be correct, continue swapping.
32+
// Note that if we encounter the duplicate, 'nums[i] != nums[correct]' would return false,
33+
// thus continuing to the next number while keeping the duplicate number in the missing position.
4734
if (nums[i] != nums[correct]) {
4835
swap(nums, i, correct);
4936
}
5037
else {
5138
i++;
5239
}
5340
}
54-
// Note that result[0] is the duplicate integer, result[1] is the missing integer.
55-
int[] result = new int[2];
41+
// Optional to use result = new int[], with result[0] == duplicate, and result[1] == missing.
42+
// Here, we use separate variable for readability.
43+
int duplicate = 0;
44+
int missing = 0;
5645
// Traverse the "sorted" array once more to find the integer that is not in its position.
5746
for (int j = 0; j < nums.length; j++) {
5847
if (j + 1 != nums[j]) {
5948
// nums[j] is the duplicate.
60-
result[0] = nums[j];
49+
duplicate = nums[j];
6150
// j + 1 is the missing number.
62-
result[1] = j + 1;
51+
missing = j + 1;
6352
break;
6453
}
6554
}
66-
return result;
55+
return new int[]{duplicate, missing};
6756
}
6857

6958
// Method to swap elements.

0 commit comments

Comments
 (0)