Skip to content

Commit 4605089

Browse files
committed
645 + README
1 parent 7706f3f commit 4605089

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

Array/645. Set Mismatch/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Time spent : 20 min(Self + see hint)
2+
3+
GIVEN: an array
4+
5+
RETURNS: duplicate num and missing num
6+
7+
EXAMPLES:
8+
9+
```
10+
Input: nums = [1,2,2,4]
11+
Output: [2,3]
12+
```
13+
14+
15+
16+
Use the kind of vote progress. Make an integer flag to represent if the num has occured before. If flag is true, then the num is duplicate one.

Array/645. Set Mismatch/Solution.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 9ms
2+
class Solution {
3+
public int[] findErrorNums(int[] nums) {
4+
int[] result = new int[2];
5+
int[] numsFlag = new int[nums.length + 1];
6+
for (int i = 0; i < nums.length; i++) {
7+
if (numsFlag[nums[i]] == -1) {
8+
result[0] = nums[i];
9+
}
10+
else {
11+
numsFlag[nums[i]] = -1;
12+
}
13+
}
14+
for (int i = 1; i <= numsFlag.length; i++) {
15+
if (numsFlag[i] == 0) {
16+
result[1] = i;
17+
break;
18+
}
19+
}
20+
return result;
21+
}
22+
}
23+
24+
// 58ms
25+
public class Solution {
26+
public int[] findErrorNums(int[] nums) {
27+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
28+
int n = nums.length;
29+
int duplicate = 0;
30+
long sum = (n * (n + 1)) / 2;
31+
for (int i : nums) {
32+
map.put(i, map.getOrDefault(i, 0) + 1);
33+
if (map.get(i) == 2) {
34+
duplicate = i;
35+
}
36+
sum = sum - i;
37+
}
38+
return new int[]{duplicate, (int)sum + duplicate};
39+
}
40+
}

Pending/645.set-mismatch.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
| 100 | 438 | [Find All Anagrams in a String][438] | Java | Easy | 2017.7.21 | HashMap | |
105105
| 101 | 599 | [Minimum Index Sum of Two Lists][599] | Java | Easy | 2017.7.21 | HashMap | |
106106
| 102 | 205 | [Isomorphic Strings][205] | Java | Easy | 2017.7.23 | Hash Table | 2018.1.7 |
107-
| 103 | 645 | [Set Mismatch][645] | Java | Easy | 2017.7.23 | HashMap | |
107+
| 103 | 645 | [Set Mismatch][645] | Java | Easy | 2017.7.23 | HashMap, Array | 2018.1.14 |
108108
| 104 | 204 | [Count Primes][204] | Java | Easy | 2017.7.24 | HashMap, Math | 2018.1.7 |
109109
| 105 | 290 | [Word Pattern][290] | Java | Easy | 2017.7.24 | HashTable | 2018.1.7 |
110110
| 106 | 12 | [Integer to Roman][12] | Java | Medium | 2017.7.27 | Math, String | 2018.1.4 |

0 commit comments

Comments
 (0)