Skip to content

Commit 44db05f

Browse files
authored
feat: add solutions to lc problems: No.3058,3059 (doocs#2384)
1 parent 33c4010 commit 44db05f

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

solution/3000-3099/3058.Friends With No Mutual Friends/README.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,32 @@ Output table is ordered by user_id1 in ascending order.</pre>
6262

6363
## 解法
6464

65-
### 方法一
65+
### 方法一:子查询
66+
67+
我们先把所有的朋友关系都列出来,记录在 `T` 表中。然后再找出没有共同朋友的朋友对。
68+
69+
接下来,我们可以使用子查询来找出没有共同朋友的朋友对,即这个朋友对不属于其他某个人的朋友。
6670

6771
<!-- tabs:start -->
6872

6973
```sql
70-
74+
# Write your MySQL query statement below
75+
WITH
76+
T AS (
77+
SELECT user_id1, user_id2 FROM Friends
78+
UNION ALL
79+
SELECT user_id2, user_id1 FROM Friends
80+
)
81+
SELECT user_id1, user_id2
82+
FROM Friends
83+
WHERE
84+
(user_id1, user_id2) NOT IN (
85+
SELECT t1.user_id1, t2.user_id1
86+
FROM
87+
T AS t1
88+
JOIN T AS t2 ON t1.user_id2 = t2.user_id2
89+
)
90+
ORDER BY 1, 2;
7191
```
7292

7393
<!-- tabs:end -->

solution/3000-3099/3058.Friends With No Mutual Friends/README_EN.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,32 @@ Output table is ordered by user_id1 in ascending order.</pre>
6060

6161
## Solutions
6262

63-
### Solution 1
63+
### Solution 1: Subquery
64+
65+
First, we list all the friend relationships and record them in table `T`. Then we find the pairs of friends who do not have common friends.
66+
67+
Next, we can use a subquery to find pairs of friends who do not have common friends, i.e., this pair of friends does not belong to any other person's friends.
6468

6569
<!-- tabs:start -->
6670

6771
```sql
68-
72+
# Write your MySQL query statement below
73+
WITH
74+
T AS (
75+
SELECT user_id1, user_id2 FROM Friends
76+
UNION ALL
77+
SELECT user_id2, user_id1 FROM Friends
78+
)
79+
SELECT user_id1, user_id2
80+
FROM Friends
81+
WHERE
82+
(user_id1, user_id2) NOT IN (
83+
SELECT t1.user_id1, t2.user_id1
84+
FROM
85+
T AS t1
86+
JOIN T AS t2 ON t1.user_id2 = t2.user_id2
87+
)
88+
ORDER BY 1, 2;
6989
```
7090

7191
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT user_id1, user_id2 FROM Friends
5+
UNION ALL
6+
SELECT user_id2, user_id1 FROM Friends
7+
)
8+
SELECT user_id1, user_id2
9+
FROM Friends
10+
WHERE
11+
(user_id1, user_id2) NOT IN (
12+
SELECT t1.user_id1, t2.user_id1
13+
FROM
14+
T AS t1
15+
JOIN T AS t2 ON t1.user_id2 = t2.user_id2
16+
)
17+
ORDER BY 1, 2;

solution/3000-3099/3059.Find All Unique Email Domains/README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,19 @@ Output table is ordered by email_domains in ascending order.
5757

5858
## 解法
5959

60-
### 方法一
60+
### 方法一:使用 `SUBSTRING_INDEX` 函数 + 分组统计
61+
62+
我们先筛选出所有以 `.com` 结尾的邮箱,然后使用 `SUBSTRING_INDEX` 函数提取出邮箱的域名,最后使用 `GROUP BY` 统计每个域名的个数。
6163

6264
<!-- tabs:start -->
6365

6466
```sql
65-
67+
# Write your MySQL query statement below
68+
SELECT SUBSTRING_INDEX(email, '@', -1) AS email_domain, COUNT(1) AS count
69+
FROM Emails
70+
WHERE email LIKE '%.com'
71+
GROUP BY 1
72+
ORDER BY 1;
6673
```
6774

6875
<!-- tabs:end -->

solution/3000-3099/3059.Find All Unique Email Domains/README_EN.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,19 @@ Output table is ordered by email_domains in ascending order.
5555

5656
## Solutions
5757

58-
### Solution 1
58+
### Solution 1: Using `SUBSTRING_INDEX` Function + Grouping Statistics
59+
60+
First, we filter out all emails ending with `.com`, then use the `SUBSTRING_INDEX` function to extract the domain name of the email. Finally, we use `GROUP BY` to count the number of each domain.
5961

6062
<!-- tabs:start -->
6163

6264
```sql
63-
65+
# Write your MySQL query statement below
66+
SELECT SUBSTRING_INDEX(email, '@', -1) AS email_domain, COUNT(1) AS count
67+
FROM Emails
68+
WHERE email LIKE '%.com'
69+
GROUP BY 1
70+
ORDER BY 1;
6471
```
6572

6673
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write your MySQL query statement below
2+
SELECT SUBSTRING_INDEX(email, '@', -1) AS email_domain, COUNT(1) AS count
3+
FROM Emails
4+
WHERE email LIKE '%.com'
5+
GROUP BY 1
6+
ORDER BY 1;

0 commit comments

Comments
 (0)