Skip to content

Commit 0c24e5b

Browse files
authored
feat: update solutions to lc problems: No.0197,0584,1581,1683,1757 (doocs#1828)
1 parent 00e8b5d commit 0c24e5b

File tree

12 files changed

+87
-51
lines changed

12 files changed

+87
-51
lines changed

solution/0100-0199/0197.Rising Temperature/README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Weather 表:</code>
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:自连接 + DATEDIFF/SUBDATE 函数**
65+
66+
我们可以通过自连接的方式,将 `Weather` 表中的每一行与它的前一行进行比较,如果温度更高,并且日期相差一天,那么就是我们要找的结果。
67+
6468
<!-- tabs:start -->
6569

6670
### **SQL**
@@ -75,13 +79,12 @@ FROM
7579
```
7680

7781
```sql
78-
SELECT
79-
w2.id AS Id
82+
# Write your MySQL query statement below
83+
SELECT w1.id
8084
FROM
81-
weather AS w1
82-
JOIN weather AS w2 ON DATE_ADD( w1.recordDate, INTERVAL 1 DAY) = w2.recordDate
83-
WHERE
84-
w1.temperature < w2.temperature
85+
Weather AS w1
86+
JOIN Weather AS w2
87+
ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature;
8588
```
8689

8790
<!-- tabs:end -->

solution/0100-0199/0197.Rising Temperature/README_EN.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ In 2015-01-04, the temperature was higher than the previous day (20 -&gt; 30).
5454

5555
## Solutions
5656

57+
**Solution 1: Self-Join + DATEDIFF/SUBDATE Function**
58+
59+
We can use self-join to compare each row in the `Weather` table with its previous row. If the temperature is higher and the date difference is one day, then it is the result we are looking for.
60+
5761
<!-- tabs:start -->
5862

5963
### **SQL**
@@ -68,13 +72,12 @@ FROM
6872
```
6973

7074
```sql
71-
SELECT
72-
w2.id AS Id
75+
# Write your MySQL query statement below
76+
SELECT w1.id
7377
FROM
74-
weather AS w1
75-
JOIN weather AS w2 ON DATE_ADD( w1.recordDate, INTERVAL 1 DAY) = w2.recordDate
76-
WHERE
77-
w1.temperature < w2.temperature
78+
Weather AS w1
79+
JOIN Weather AS w2
80+
ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature;
7881
```
7982

8083
<!-- tabs:end -->

solution/0100-0199/0197.Rising Temperature/Solution.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ SELECT w1.id
33
FROM
44
Weather AS w1
55
JOIN Weather AS w2
6-
ON DATEDIFF(w1.recordDate, w2.recordDate) = 1 AND w1.temperature > w2.temperature;
6+
ON SUBDATE(w1.recordDate, 1) = w2.recordDate AND w1.temperature > w2.temperature;

solution/0500-0599/0584.Find Customer Referee/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Customer 表:
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:条件过滤**
60+
61+
我们可以直接筛选出 `referee_id` 不为 `2` 的客户姓名。注意,`referee_id``NULL` 的客户也应该被筛选出来。
62+
5963
<!-- tabs:start -->
6064

6165
### **SQL**

solution/0500-0599/0584.Find Customer Referee/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Customer table:
5555

5656
## Solutions
5757

58+
**Solution 1: Conditional Filtering**
59+
60+
We can directly filter out the customer names whose `referee_id` is not `2`. Note that the customers whose `referee_id` is `NULL` should also be filtered out.
61+
5862
<!-- tabs:start -->
5963

6064
### **SQL**

solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,36 @@ ID = 96 的顾客曾经去过购物中心,并且没有进行任何交易。
9191

9292
<!-- 这里可写通用的实现逻辑 -->
9393

94+
**方法一:子查询 + 分组统计**
95+
96+
我们可以使用子查询,先找出所有没有进行交易的 `visit_id`,然后按照 `customer_id` 进行分组,统计每个顾客的没有进行交易的次数。
97+
98+
**方法二:左连接 + 分组统计**
99+
100+
我们也可以使用左连接,将 `Visits` 表和 `Transactions` 表按照 `visit_id` 进行连接,然后筛选出 `amount``NULL` 的记录,按照 `customer_id` 进行分组,统计每个顾客的没有进行交易的次数。
101+
94102
<!-- tabs:start -->
95103

96104
### **SQL**
97105

98106
<!-- 这里可写当前语言的特殊实现逻辑 -->
99107

100108
```sql
101-
SELECT
102-
customer_id,
103-
COUNT(*) AS count_no_trans
109+
# Write your MySQL query statement below
110+
SELECT customer_id, COUNT(1) AS count_no_trans
104111
FROM Visits
105-
WHERE
106-
visit_id NOT IN (
107-
SELECT visit_id
108-
FROM Transactions
109-
)
110-
GROUP BY customer_id;
112+
WHERE visit_id NOT IN (SELECT visit_id FROM Transactions)
113+
GROUP BY 1;
111114
```
112115

113116
```sql
114117
# Write your MySQL query statement below
115118
SELECT customer_id, COUNT(1) AS count_no_trans
116119
FROM
117-
Visits AS v
118-
LEFT JOIN Transactions AS t ON v.visit_id = t.visit_id
120+
Visits
121+
LEFT JOIN Transactions USING (visit_id)
119122
WHERE amount IS NULL
120-
GROUP BY customer_id;
123+
GROUP BY 1;
121124
```
122125

123126
<!-- tabs:end -->

solution/1500-1599/1581.Customer Who Visited but Did Not Make Any Transactions/README_EN.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -87,33 +87,36 @@ As we can see, users with IDs 30 and 96 visited the mall one time without making
8787

8888
## Solutions
8989

90+
**Solution 1: Subquery + Grouping**
91+
92+
We can use a subquery to first find all `visit_id`s that have not made any transactions, and then group by `customer_id` to count the number of times each customer has not made any transactions.
93+
94+
**Solution 2: Left Join + Grouping**
95+
96+
We can also use a left join to join the `Visits` table and the `Transactions` table on `visit_id`, and then filter out the records where `amount` is `NULL`. After that, we can group by `customer_id` to count the number of times each customer has not made any transactions.
97+
9098
<!-- tabs:start -->
9199

92100
### **SQL**
93101

94102
<!-- 这里可写当前语言的特殊实现逻辑 -->
95103

96104
```sql
97-
SELECT
98-
customer_id,
99-
COUNT(*) AS count_no_trans
105+
# Write your MySQL query statement below
106+
SELECT customer_id, COUNT(1) AS count_no_trans
100107
FROM Visits
101-
WHERE
102-
visit_id NOT IN (
103-
SELECT visit_id
104-
FROM Transactions
105-
)
106-
GROUP BY customer_id;
108+
WHERE visit_id NOT IN (SELECT visit_id FROM Transactions)
109+
GROUP BY 1;
107110
```
108111

109112
```sql
110113
# Write your MySQL query statement below
111114
SELECT customer_id, COUNT(1) AS count_no_trans
112115
FROM
113-
Visits AS v
114-
LEFT JOIN Transactions AS t ON v.visit_id = t.visit_id
116+
Visits
117+
LEFT JOIN Transactions USING (visit_id)
115118
WHERE amount IS NULL
116-
GROUP BY customer_id;
119+
GROUP BY 1;
117120
```
118121

119122
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
SELECT
2-
customer_id,
3-
COUNT(*) AS count_no_trans
4-
FROM Visits
5-
WHERE
6-
visit_id NOT IN (
7-
SELECT visit_id
8-
FROM Transactions
9-
)
10-
GROUP BY customer_id;
1+
# Write your MySQL query statement below
2+
SELECT customer_id, COUNT(1) AS count_no_trans
3+
FROM
4+
Visits
5+
LEFT JOIN Transactions USING (visit_id)
6+
WHERE amount IS NULL
7+
GROUP BY 1;

solution/1600-1699/1683.Invalid Tweets/README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ Tweets 表:
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
- `CHAR_LENGTH(str)`: 中文、数字、字母都是 1 字节
59-
- `LENGTH(str)`:
60-
- utf8: 中文 3 字节,数字、字母 1 字节
61-
- gbk: 中文 2 字节,数字、字母 1 字节
58+
**方法一:使用 `CHAR_LENGTH` 函数**
59+
60+
`CHAR_LENGTH()` 函数返回字符串的长度,其中中文、数字、字母都是 $1$ 字节。
61+
62+
`LENGTH()` 函数返回字符串的长度,其中 utf8 编码下,中文 $3$ 字节,数字、字母 $1$ 字节;gbk 编码下,中文 $2$ 字节,数字、字母 $1$ 字节。
63+
64+
对于本题,我们直接用 `CHAR_LENGTH` 函数获取字符串长度,筛选出长度大于 $15$ 的推文 ID 即可。
6265

6366
<!-- tabs:start -->
6467

solution/1600-1699/1683.Invalid Tweets/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ Tweet 2 has length = 32. It is an invalid tweet.
5050

5151
## Solutions
5252

53+
**Solution 1: Using `CHAR_LENGTH` Function**
54+
55+
The `CHAR_LENGTH()` function returns the length of a string, where Chinese characters, numbers, and letters are all counted as $1$ byte.
56+
57+
The `LENGTH()` function returns the length of a string, where under utf8 encoding, Chinese characters are counted as $3$ bytes, while numbers and letters are counted as $1$ byte; under gbk encoding, Chinese characters are counted as $2$ bytes, while numbers and letters are counted as $1$ byte.
58+
59+
For this problem, we can directly use the `CHAR_LENGTH` function to get the length of the string, and filter out the tweet IDs with a length greater than $15$.
60+
5361
<!-- tabs:start -->
5462

5563
### **SQL**

solution/1700-1799/1757.Recyclable and Low Fat Products/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ Products 表:
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:条件筛选**
63+
64+
我们直接筛选出 `low_fats``Y``recyclable``Y` 的产品编号即可。
65+
6266
<!-- tabs:start -->
6367

6468
### **SQL**

solution/1700-1799/1757.Recyclable and Low Fat Products/README_EN.md

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Products table:
5353

5454
## Solutions
5555

56+
**Solution 1: Conditional Filtering**
57+
58+
We can directly filter the product IDs where `low_fats` is `Y` and `recyclable` is `Y`.
59+
5660
<!-- tabs:start -->
5761

5862
### **SQL**

0 commit comments

Comments
 (0)