Skip to content

Commit 30bedef

Browse files
committed
day 2
1 parent 3e5094c commit 30bedef

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Set Mismatch
3+
============
4+
5+
You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.
6+
7+
You are given an integer array nums representing the data status of this set after the error.
8+
9+
Find the number that occurs twice and the number that is missing and return them in the form of an array.
10+
11+
Example 1:
12+
Input: nums = [1,2,2,4]
13+
Output: [2,3]
14+
15+
Example 2:
16+
Input: nums = [1,1]
17+
Output: [1,2]
18+
19+
Constraints:
20+
2 <= nums.length <= 104
21+
1 <= nums[i] <= 104
22+
*/
23+
24+
class Solution
25+
{
26+
public:
27+
vector<int> findErrorNums(vector<int> &nums)
28+
{
29+
unordered_map<int, int> freq;
30+
vector<int> ans;
31+
32+
for (auto &i : nums)
33+
freq[i]++;
34+
for (auto &i : freq)
35+
if (i.second == 2)
36+
ans.push_back(i.first);
37+
for (auto i = 1; i <= nums.size(); ++i)
38+
if (freq[i] == 0)
39+
ans.push_back(i);
40+
41+
return ans;
42+
}
43+
};

Leetcode Daily Challenge/March-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
| Day | Question Links | Solutions |
44
| :-: | :------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------: |
55
| 1. | [Distribute Candies](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/588/week-1-march-1st-march-7th/3657/) | [cpp](./01.%20Distribute%20Candies.cpp) |
6+
| 2. | [Set Mismatch](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/588/week-1-march-1st-march-7th/3658/) | [cpp](./02.%20Set%20Mismatch.cpp) |

0 commit comments

Comments
 (0)