Skip to content

Commit 6d69297

Browse files
committed
day 3
1 parent 30bedef commit 6d69297

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Missing Number
3+
==============
4+
5+
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
6+
7+
Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity?
8+
9+
Example 1:
10+
Input: nums = [3,0,1]
11+
Output: 2
12+
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
13+
14+
Example 2:
15+
Input: nums = [0,1]
16+
Output: 2
17+
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
18+
19+
Example 3:
20+
Input: nums = [9,6,4,2,3,5,7,0,1]
21+
Output: 8
22+
Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
23+
24+
Example 4:
25+
Input: nums = [0]
26+
Output: 1
27+
Explanation: n = 1 since there is 1 number, so all numbers are in the range [0,1]. 1 is the missing number in the range since it does not appear in nums.
28+
29+
Constraints:
30+
n == nums.length
31+
1 <= n <= 104
32+
0 <= nums[i] <= n
33+
All the numbers of nums are unique.
34+
*/
35+
36+
class Solution
37+
{
38+
public:
39+
int missingNumber(vector<int> &nums)
40+
{
41+
unordered_map<int, int> m;
42+
for (auto &i : nums)
43+
m[i]++;
44+
for (int i = 0; i <= nums.size(); ++i)
45+
{
46+
if (m[i] == 0)
47+
return i;
48+
}
49+
return -1;
50+
}
51+
};

Leetcode Daily Challenge/March-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
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) |
66
| 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) |
7+
| 3. | [Missing Number](https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/588/week-1-march-1st-march-7th/3659/) | [cpp](./03.%20Missing%20Number.cpp) |

0 commit comments

Comments
 (0)