Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 24f6ea6

Browse files
committed
Add solution #645
1 parent 09dd2fe commit 24f6ea6

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

0645-set-mismatch.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 645. Set Mismatch
3+
* https://leetcode.com/problems/set-mismatch/
4+
* Difficulty: Medium
5+
*
6+
* You have a set of integers s, which originally contains all the numbers from 1 to n.
7+
* Unfortunately, due to some error, one of the numbers in s got duplicated to another
8+
* number in the set, which results in repetition of one number and loss of another number.
9+
*
10+
* You are given an integer array nums representing the data status of this set after
11+
* the error.
12+
*
13+
* Find the number that occurs twice and the number that is missing and return them in
14+
* the form of an array.
15+
*/
16+
17+
/**
18+
* @param {number[]} nums
19+
* @return {number[]}
20+
*/
21+
var findErrorNums = function(nums) {
22+
const set = new Set();
23+
const result = [];
24+
25+
for (const n of nums) {
26+
if (set.has(n)) {
27+
result.push(n);
28+
}
29+
set.add(n);
30+
}
31+
32+
for (let i = nums.length; i > 0; i--) {
33+
if (!set.has(i)) {
34+
result.push(i);
35+
break;
36+
}
37+
}
38+
39+
return result;
40+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
606|[Construct String from Binary Tree](./0606-construct-string-from-binary-tree.js)|Easy|
133133
617|[Merge Two Binary Trees](./0617-merge-two-binary-trees.js)|Easy|
134134
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
135+
645|[Set Mismatch](./0645-set-mismatch.js)|Medium|
135136
648|[Replace Words](./0648-replace-words.js)|Medium|
136137
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv-input-is-a-bst.js)|Easy|
137138
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|

0 commit comments

Comments
 (0)