Skip to content

Commit e8230a0

Browse files
zhouboyi1998zhouboyi
authored andcommitted
docs(note): 添加题解:196. 删除重复的电子邮箱
1 parent 9689709 commit e8230a0

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<h1 style="text-align: center;"> <span style="color: #00AF9B;">196. 删除重复的电子邮箱</span> </h1>
2+
3+
### 🚀 LeetCode
4+
5+
<base target="_blank">
6+
7+
<span style="color: #00AF9B;">**Easy**</span> [**https://leetcode.cn/problems/delete-duplicate-emails/**](https://leetcode.cn/problems/delete-duplicate-emails/)
8+
9+
---
10+
11+
### ❓ Description
12+
13+
<br/>
14+
15+
**表:** `Person`
16+
17+
```
18+
+-------------+---------+
19+
| Column Name | Type |
20+
+-------------+---------+
21+
| id | int |
22+
| email | varchar |
23+
+-------------+---------+
24+
id 是该表的主键列 (具有唯一值的列).
25+
该表的每一行包含一封电子邮件.
26+
电子邮件将不包含大写字母.
27+
```
28+
29+
<br/>
30+
31+
编写解决方案 **删除** 所有重复的电子邮件,只保留一个具有最小 `id` 的唯一电子邮件。
32+
33+
(对于 `SQL` 用户,请注意你应该编写一个 `DELETE` 语句而不是 `SELECT` 语句。)
34+
35+
(对于 `Pandas` 用户,请注意你应该直接修改 `Person` 表。)
36+
37+
运行脚本后,显示的答案是 `Person` 表。
38+
39+
驱动程序将首先编译并运行您的代码片段,然后再显示 `Person` 表。
40+
41+
`Person` 表的最终顺序 **无关紧要**
42+
43+
返回结果格式如下示例所示。
44+
45+
**示例 1:**
46+
47+
```
48+
输入:
49+
Person 表:
50+
+----+------------------+
51+
| id | email |
52+
+----+------------------+
53+
| 1 | john@example.com |
54+
| 2 | bob@example.com |
55+
| 3 | john@example.com |
56+
+----+------------------+
57+
输出:
58+
+----+------------------+
59+
| id | email |
60+
+----+------------------+
61+
| 1 | john@example.com |
62+
| 2 | bob@example.com |
63+
+----+------------------+
64+
解释: john@example.com 重复两次, 我们保留最小的 Id = 1.
65+
```
66+
67+
<br/>
68+
69+
**SQL Schema**
70+
71+
```
72+
Create table If Not Exists Person (Id int, Email varchar(255));
73+
Truncate table Person;
74+
insert into Person (id, email) values ('1', 'john@example.com');
75+
insert into Person (id, email) values ('2', 'bob@example.com');
76+
insert into Person (id, email) values ('3', 'john@example.com');
77+
```
78+
79+
<br/>
80+
81+
**Pandas Schema**
82+
83+
```
84+
data = [[1, 'john@example.com'], [2, 'bob@example.com'], [3, 'john@example.com']]
85+
person = pd.DataFrame(data, columns=['id', 'email']).astype({'id':'int64', 'email':'object'})
86+
```
87+
88+
---
89+
90+
### ❗ Solution(`SELF JOIN` 自连接)
91+
92+
<br/>
93+
94+
#### MySQL
95+
96+
```
97+
DELETE p1
98+
FROM person AS p1, person AS p2
99+
WHERE p1.email = p2.email
100+
AND p1.id > p2.id;
101+
```
102+
103+
---
104+
105+
### ❗ Solution(`ROW_NUMBER()` 函数)
106+
107+
<br/>
108+
109+
#### MySQL
110+
111+
```
112+
WITH temp AS (
113+
SELECT id, email,
114+
ROW_NUMBER() OVER (PARTITION BY email ORDER BY id ASC) AS rn
115+
FROM person
116+
)
117+
DELETE FROM person
118+
WHERE id NOT IN (
119+
SELECT id FROM temp WHERE rn = 1
120+
);
121+
```
122+
123+
<br/>
124+
125+
#### PostgreSQL
126+
127+
```
128+
WITH temp AS (
129+
SELECT id, email,
130+
ROW_NUMBER() OVER (PARTITION BY email ORDER BY id ASC) AS rn
131+
FROM person
132+
)
133+
DELETE FROM person
134+
WHERE id NOT IN (
135+
SELECT id FROM temp WHERE rn = 1
136+
);
137+
```
138+
139+
---
140+
141+
### ❗ Solution(`Pandas`
142+
143+
<br/>
144+
145+
#### Python
146+
147+
```
148+
import pandas as pd
149+
150+
def delete_duplicate_emails(person: pd.DataFrame) -> None:
151+
min_id = person.groupby('email')['id'].transform('min')
152+
remove_person = person[person['id'] != min_id]
153+
person.drop(remove_person.index, inplace=True)
154+
return
155+
```

markdown/hello/0000.Hello.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
| <span style="color: #00AF9B;">**Easy**</span> | [**0182.查找重复的电子邮箱**](../easy/0182.查找重复的电子邮箱.md) |
6767
| <span style="color: #00AF9B;">**Easy**</span> | [**0183.从不订购的客户**](../easy/0183.从不订购的客户.md) |
6868
| <span style="color: #00AF9B;">**Easy**</span> | [**0191.位1的个数**](../easy/0191.位1的个数.md) |
69+
| <span style="color: #00AF9B;">**Easy**</span> | [**0196.删除重复的电子邮箱**](../easy/0196.删除重复的电子邮箱.md) |
6970
| <span style="color: #00AF9B;">**Easy**</span> | [**0197.上升的温度**](../easy/0197.上升的温度.md) |
7071
| <span style="color: #FFB822;">**Medium**</span> | [**0198.打家劫舍**](../medium/0198.打家劫舍.md) |
7172
| <span style="color: #00AF9B;">**Easy**</span> | [**0206.反转链表**](../easy/0206.反转链表.md) |

0 commit comments

Comments
 (0)