Skip to content

Commit 39cc753

Browse files
committed
feat: add weekly contest 474
1 parent 3074e75 commit 39cc753

File tree

26 files changed

+1612
-2
lines changed

26 files changed

+1612
-2
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3731.Find%20Missing%20Elements/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3731. 找出缺失的元素](https://leetcode.cn/problems/find-missing-elements)
10+
11+
[English Version](/solution/3700-3799/3731.Find%20Missing%20Elements/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>nums</code> ,数组由若干&nbsp;<b>互不相同</b> 的整数组成。</p>
18+
19+
<p>数组 <code>nums</code> 原本包含了某个范围内的&nbsp;<strong>所有整数&nbsp;</strong>。但现在,其中可能 <strong>缺失</strong> 部分整数。</p>
20+
21+
<p>该范围内的&nbsp;<strong>最小&nbsp;</strong>整数和&nbsp;<strong>最大&nbsp;</strong>整数仍然存在于 <code>nums</code> 中。</p>
22+
23+
<p>返回一个&nbsp;<strong>有序&nbsp;</strong>列表,包含该范围内缺失的所有整数,并&nbsp;<strong>按从小到大排序</strong>。如果没有缺失的整数,返回一个&nbsp;<strong>空&nbsp;</strong>列表。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong class="example">示例 1:</strong></p>
28+
29+
<div class="example-block">
30+
<p><strong>输入:</strong> <span class="example-io">nums = [1,4,2,5]</span></p>
31+
32+
<p><strong>输出:</strong> <span class="example-io">[3]</span></p>
33+
34+
<p><strong>解释:</strong></p>
35+
36+
<p>最小整数为 1,最大整数为 5,因此完整的范围应为 <code>[1,2,3,4,5]</code>。其中只有 3 缺失。</p>
37+
</div>
38+
39+
<p><strong class="example">示例 2:</strong></p>
40+
41+
<div class="example-block">
42+
<p><strong>输入:</strong> <span class="example-io">nums = [7,8,6,9]</span></p>
43+
44+
<p><strong>输出:</strong> <span class="example-io">[]</span></p>
45+
46+
<p><strong>解释:</strong></p>
47+
48+
<p>最小整数为 6,最大整数为 9,因此完整的范围为 <code>[6,7,8,9]</code>。所有整数均已存在,因此没有缺失的整数。</p>
49+
</div>
50+
51+
<p><strong class="example">示例 3:</strong></p>
52+
53+
<div class="example-block">
54+
<p><strong>输入:</strong> <span class="example-io">nums = [5,1]</span></p>
55+
56+
<p><strong>输出:</strong> <span class="example-io">[2,3,4]</span></p>
57+
58+
<p><strong>解释:</strong></p>
59+
60+
<p>最小整数为 1,最大整数为 5,因此完整的范围应为 <code>[1,2,3,4,5]</code>。缺失的整数为 2、3 和 4。</p>
61+
</div>
62+
63+
<p>&nbsp;</p>
64+
65+
<p><strong>提示:</strong></p>
66+
67+
<ul>
68+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
69+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
70+
</ul>
71+
72+
<!-- description:end -->
73+
74+
## 解法
75+
76+
<!-- solution:start -->
77+
78+
### 方法一:哈希表
79+
80+
我们先找出数组 $\textit{nums}$ 中的最小值和最大值,记为 $\textit{mn}$ 和 $\textit{mx}$。然后我们使用哈希表存储数组 $\textit{nums}$ 中的所有元素。
81+
82+
接下来,我们遍历区间 $[\textit{mn} + 1, \textit{mx} - 1]$,对于每个整数 $x$,如果 $x$ 不在哈希表中,我们就将其加入答案列表中。
83+
84+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。
85+
86+
<!-- tabs:start -->
87+
88+
#### Python3
89+
90+
```python
91+
class Solution:
92+
def findMissingElements(self, nums: List[int]) -> List[int]:
93+
mn, mx = min(nums), max(nums)
94+
s = set(nums)
95+
return [x for x in range(mn + 1, mx) if x not in s]
96+
```
97+
98+
#### Java
99+
100+
```java
101+
class Solution {
102+
public List<Integer> findMissingElements(int[] nums) {
103+
int mn = 100, mx = 0;
104+
Set<Integer> s = new HashSet<>();
105+
for (int x : nums) {
106+
mn = Math.min(mn, x);
107+
mx = Math.max(mx, x);
108+
s.add(x);
109+
}
110+
List<Integer> ans = new ArrayList<>();
111+
for (int x = mn + 1; x < mx; ++x) {
112+
if (!s.contains(x)) {
113+
ans.add(x);
114+
}
115+
}
116+
return ans;
117+
}
118+
}
119+
```
120+
121+
#### C++
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
vector<int> findMissingElements(vector<int>& nums) {
127+
int mn = 100, mx = 0;
128+
unordered_set<int> s;
129+
for (int x : nums) {
130+
mn = min(mn, x);
131+
mx = max(mx, x);
132+
s.insert(x);
133+
}
134+
vector<int> ans;
135+
for (int x = mn + 1; x < mx; ++x) {
136+
if (!s.count(x)) {
137+
ans.push_back(x);
138+
}
139+
}
140+
return ans;
141+
}
142+
};
143+
```
144+
145+
#### Go
146+
147+
```go
148+
func findMissingElements(nums []int) (ans []int) {
149+
mn, mx := 100, 0
150+
s := make(map[int]bool)
151+
for _, x := range nums {
152+
mn = min(mn, x)
153+
mx = max(mx, x)
154+
s[x] = true
155+
}
156+
for x := mn + 1; x < mx; x++ {
157+
if !s[x] {
158+
ans = append(ans, x)
159+
}
160+
}
161+
return
162+
}
163+
```
164+
165+
#### TypeScript
166+
167+
```ts
168+
function findMissingElements(nums: number[]): number[] {
169+
let [mn, mx] = [100, 0];
170+
const s = new Set<number>();
171+
for (const x of nums) {
172+
mn = Math.min(mn, x);
173+
mx = Math.max(mx, x);
174+
s.add(x);
175+
}
176+
const ans: number[] = [];
177+
for (let x = mn + 1; x < mx; ++x) {
178+
if (!s.has(x)) {
179+
ans.push(x);
180+
}
181+
}
182+
return ans;
183+
}
184+
```
185+
186+
<!-- tabs:end -->
187+
188+
<!-- solution:end -->
189+
190+
<!-- problem:end -->
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3731.Find%20Missing%20Elements/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3731. Find Missing Elements](https://leetcode.com/problems/find-missing-elements)
10+
11+
[中文文档](/solution/3700-3799/3731.Find%20Missing%20Elements/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer array <code>nums</code> consisting of <strong>unique</strong> integers.</p>
18+
19+
<p>Originally, <code>nums</code> contained <strong>every integer</strong> within a certain range. However, some integers might have gone <strong>missing</strong> from the array.</p>
20+
21+
<p>The <strong>smallest</strong> and <strong>largest</strong> integers of the original range are still present in <code>nums</code>.</p>
22+
23+
<p>Return a <strong>sorted</strong> list of all the missing integers in this range. If no integers are missing, return an <strong>empty</strong> list.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,2,5]</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">[3]</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<p>The smallest integer is 1 and the largest is 5, so the full range should be <code>[1,2,3,4,5]</code>. Among these, only 3 is missing.</p>
36+
</div>
37+
38+
<p><strong class="example">Example 2:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong> <span class="example-io">nums = [7,8,6,9]</span></p>
42+
43+
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
44+
45+
<p><strong>Explanation:</strong></p>
46+
47+
<p>The smallest integer is 6 and the largest is 9, so the full range is <code>[6,7,8,9]</code>. All integers are already present, so no integer is missing.</p>
48+
</div>
49+
50+
<p><strong class="example">Example 3:</strong></p>
51+
52+
<div class="example-block">
53+
<p><strong>Input:</strong> <span class="example-io">nums = [5,1]</span></p>
54+
55+
<p><strong>Output:</strong> <span class="example-io">[2,3,4]</span></p>
56+
57+
<p><strong>Explanation:</strong></p>
58+
59+
<p>The smallest integer is 1 and the largest is 5, so the full range should be <code>[1,2,3,4,5]</code>. The missing integers are 2, 3, and 4.</p>
60+
</div>
61+
62+
<p>&nbsp;</p>
63+
<p><strong>Constraints:</strong></p>
64+
65+
<ul>
66+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
67+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
68+
</ul>
69+
70+
<!-- description:end -->
71+
72+
## Solutions
73+
74+
<!-- solution:start -->
75+
76+
### Solution 1: Hash Table
77+
78+
We first find the minimum and maximum values in the array $\textit{nums}$, denoted as $\textit{mn}$ and $\textit{mx}$. Then we use a hash table to store all elements in the array $\textit{nums}$.
79+
80+
Next, we iterate through the interval $[\textit{mn} + 1, \textit{mx} - 1]$. For each integer $x$, if $x$ is not in the hash table, we add it to the answer list.
81+
82+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
83+
84+
<!-- tabs:start -->
85+
86+
#### Python3
87+
88+
```python
89+
class Solution:
90+
def findMissingElements(self, nums: List[int]) -> List[int]:
91+
mn, mx = min(nums), max(nums)
92+
s = set(nums)
93+
return [x for x in range(mn + 1, mx) if x not in s]
94+
```
95+
96+
#### Java
97+
98+
```java
99+
class Solution {
100+
public List<Integer> findMissingElements(int[] nums) {
101+
int mn = 100, mx = 0;
102+
Set<Integer> s = new HashSet<>();
103+
for (int x : nums) {
104+
mn = Math.min(mn, x);
105+
mx = Math.max(mx, x);
106+
s.add(x);
107+
}
108+
List<Integer> ans = new ArrayList<>();
109+
for (int x = mn + 1; x < mx; ++x) {
110+
if (!s.contains(x)) {
111+
ans.add(x);
112+
}
113+
}
114+
return ans;
115+
}
116+
}
117+
```
118+
119+
#### C++
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
vector<int> findMissingElements(vector<int>& nums) {
125+
int mn = 100, mx = 0;
126+
unordered_set<int> s;
127+
for (int x : nums) {
128+
mn = min(mn, x);
129+
mx = max(mx, x);
130+
s.insert(x);
131+
}
132+
vector<int> ans;
133+
for (int x = mn + 1; x < mx; ++x) {
134+
if (!s.count(x)) {
135+
ans.push_back(x);
136+
}
137+
}
138+
return ans;
139+
}
140+
};
141+
```
142+
143+
#### Go
144+
145+
```go
146+
func findMissingElements(nums []int) (ans []int) {
147+
mn, mx := 100, 0
148+
s := make(map[int]bool)
149+
for _, x := range nums {
150+
mn = min(mn, x)
151+
mx = max(mx, x)
152+
s[x] = true
153+
}
154+
for x := mn + 1; x < mx; x++ {
155+
if !s[x] {
156+
ans = append(ans, x)
157+
}
158+
}
159+
return
160+
}
161+
```
162+
163+
#### TypeScript
164+
165+
```ts
166+
function findMissingElements(nums: number[]): number[] {
167+
let [mn, mx] = [100, 0];
168+
const s = new Set<number>();
169+
for (const x of nums) {
170+
mn = Math.min(mn, x);
171+
mx = Math.max(mx, x);
172+
s.add(x);
173+
}
174+
const ans: number[] = [];
175+
for (let x = mn + 1; x < mx; ++x) {
176+
if (!s.has(x)) {
177+
ans.push(x);
178+
}
179+
}
180+
return ans;
181+
}
182+
```
183+
184+
<!-- tabs:end -->
185+
186+
<!-- solution:end -->
187+
188+
<!-- problem:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> findMissingElements(vector<int>& nums) {
4+
int mn = 100, mx = 0;
5+
unordered_set<int> s;
6+
for (int x : nums) {
7+
mn = min(mn, x);
8+
mx = max(mx, x);
9+
s.insert(x);
10+
}
11+
vector<int> ans;
12+
for (int x = mn + 1; x < mx; ++x) {
13+
if (!s.count(x)) {
14+
ans.push_back(x);
15+
}
16+
}
17+
return ans;
18+
}
19+
};

0 commit comments

Comments
 (0)