Skip to content

Commit 2011acc

Browse files
committed
solve problem Set Mismatch
1 parent 03e1ed0 commit 2011acc

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ All solutions will be accepted!
152152
|674|[Longest Continuous Increasing Subsequence](https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/description/)|[java/py/js](./algorithms/LongestContinuousIncreasingSubsequence)|Easy|
153153
|234|[Palindrome Linked List](https://leetcode-cn.com/problems/palindrome-linked-list/description/)|[java/py/js](./algorithms/PalindromeLinkedList)|Easy|
154154
|414|[Third Maximum Number](https://leetcode-cn.com/problems/third-maximum-number/description/)|[java/py/js](./algorithms/ThirdMaximumNumber)|Easy|
155+
|645|[Set Mismatch](https://leetcode-cn.com/problems/set-mismatch/description/)|[java/py/js](./algorithms/SetMismatch)|Easy|
155156

156157
# Database
157158
|#|Title|Solution|Difficulty|

algorithms/SetMismatch/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Set Mismatch
2+
This problem is easy to solve by hashmap

algorithms/SetMismatch/Solution.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int[] findErrorNums(int[] nums) {
3+
int[] res = new int[2];
4+
Map<Integer, Boolean> valueMap = new HashMap<Integer, Boolean>();
5+
6+
for (int num : nums) {
7+
if (valueMap.get(num) == null) {
8+
valueMap.put(num, true);
9+
} else {
10+
res[0] = num;
11+
}
12+
}
13+
14+
for (int i = 1; i <= nums.length; i++) {
15+
if (valueMap.get(i) == null) {
16+
res[1] = i;
17+
}
18+
}
19+
20+
return res;
21+
}
22+
}

algorithms/SetMismatch/solution.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var findErrorNums = function(nums) {
6+
let res = [],
7+
valueMap = new Map()
8+
9+
nums.forEach(num => {
10+
if (valueMap.get(num) === undefined) {
11+
valueMap.set(num, true)
12+
} else {
13+
res.push(num)
14+
}
15+
})
16+
17+
for (let i = 1; i <= nums.length; i++) {
18+
if (valueMap.get(i) === undefined) {
19+
res.push(i)
20+
}
21+
}
22+
23+
return res
24+
};

algorithms/SetMismatch/solution.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def findErrorNums(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[int]
6+
"""
7+
res = []
8+
value_map = {}
9+
10+
for num in nums:
11+
if value_map.get(num) == None:
12+
value_map[num] = True
13+
else:
14+
res.append(num)
15+
16+
for i in range(1, len(nums) + 1):
17+
if value_map.get(i) == None:
18+
res.append(i)
19+
return res

0 commit comments

Comments
 (0)