Skip to content

Commit d51c102

Browse files
committed
feat: add new sql solution
1 parent aba84dc commit d51c102

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

solution/0100-0199/0180.Consecutive Numbers/README.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,44 @@ Result 表:
5757

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

60-
**方法一:窗口函数**
60+
**方法一:两次连接**
6161

62-
我们可以使用窗口函数 `LAG``LEAD` 来判断当前行的前一行和后一行是否与当前行的 `num` 相等,如果当前行与前一行的 `num` 相等,则记字段 $a$ 为 $1$,否则记为 $0$;如果当前行与后一行的 `num` 相等,则记字段 $b$ 为 $1$,否则记为 $0$
62+
我们可以使用两次连接来解决这个问题
6363

64-
最后,我们只需要筛选出 $a = 1$ 且 $b = 1$ 的行,即为连续出现至少三次的数字。注意,我们需要使用 `DISTINCT` 关键字来对结果去重。
64+
我们首先进行一次自连接,连接条件是 `l1.num = l2.num` 并且 `l1.id = l2.id - 1`,这样我们就可以找出所有至少连续出现两次的数字。然后,我们再进行一次自连接,连接条件是 `l2.num = l3.num` 并且 `l2.id = l3.id - 1`,这样我们就可以找出所有至少连续出现三次的数字。最后,我们只需要筛选出去重的 `l2.num` 即可。
65+
66+
**方法二:窗口函数**
67+
68+
我们可以使用窗口函数 `LAG``LEAD` 来获取上一行的 `num` 和下一行的 `num`,记录在字段 $a$ 和 $b$ 中。最后,我们只需要筛选出 $a =num$ 并且 $b = num$ 的行,这些行就是至少连续出现三次的数字。注意,我们需要使用 `DISTINCT` 关键字来对结果去重。
6569

6670
我们也可以对数字进行分组,具体做法是使用 `IF` 函数来判断当前行与前一行的 `num` 是否相等,如果相等则记为 $0$,否则记为 $1$,然后使用窗口函数 `SUM` 来计算前缀和,这样计算出的前缀和就是分组的标识。最后,我们只需要按照分组标识进行分组,然后筛选出每组中的行数大于等于 $3$ 的数字即可。同样,我们需要使用 `DISTINCT` 关键字来对结果去重。
6771

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

7074
### **SQL**
7175

76+
```sql
77+
# Write your MySQL query statement below
78+
SELECT DISTINCT l2.num AS ConsecutiveNums
79+
FROM
80+
Logs AS l1
81+
JOIN Logs AS l2 ON l1.id = l2.id - 1 AND l1.num = l2.num
82+
JOIN Logs AS l3 ON l2.id = l3.id - 1 AND l2.num = l3.num;
83+
```
84+
7285
```sql
7386
# Write your MySQL query statement below
7487
WITH
7588
T AS (
7689
SELECT
7790
*,
78-
num = (LAG(num) OVER (ORDER BY id)) AS a,
79-
num = (LEAD(num) OVER (ORDER BY id)) AS b
91+
LAG(num) OVER (ORDER BY id) AS a,
92+
LEAD(num) OVER (ORDER BY id) AS b
8093
FROM Logs
8194
)
8295
SELECT DISTINCT num AS ConsecutiveNums
8396
FROM T
84-
WHERE a = 1 AND b = 1;
97+
WHERE a = num AND b = num;
8598
```
8699

87100
```sql

solution/0100-0199/0180.Consecutive Numbers/README_EN.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,44 @@ Logs table:
5353

5454
## Solutions
5555

56-
**Solution 1: Window Function**
56+
**Solution 1: Two Joins**
5757

58-
We can use the window functions `LAG` and `LEAD` to determine whether the previous row and the next row of the current row are equal to the `num` of the current row. If the `num` of the current row is equal to the `num` of the previous row, we set the field $a$ to $1$, otherwise we set it to $0$; if the `num` of the current row is equal to the `num` of the next row, we set the field $b$ to $1$, otherwise we set it to $0$.
58+
We can use two joins to solve this problem.
5959

60-
Finally, we only need to filter out the rows where $a = 1$ and $b = 1$, which are the numbers that appear at least three times in a row. Note that we need to use the `DISTINCT` keyword to remove duplicates from the results.
60+
First, we perform a self-join with the condition `l1.num = l2.num` and `l1.id = l2.id - 1`, so that we can find all numbers that appear at least twice in a row. Then, we perform another self-join with the condition `l2.num = l3.num` and `l2.id = l3.id - 1`, so that we can find all numbers that appear at least three times in a row. Finally, we only need to select the distinct `l2.num`.
61+
62+
**Solution 2: Window Function**
63+
64+
We can use the window functions `LAG` and `LEAD` to obtain the `num` of the previous row and the next row of the current row, and record them in the fields $a$ and $b$, respectively. Finally, we only need to filter out the rows where $a = num$ and $b = num$, which are the numbers that appear at least three times in a row. Note that we need to use the `DISTINCT` keyword to remove duplicates from the results.
6165

6266
We can also group the numbers by using the `IF` function to determine whether the `num` of the current row is equal to the `num` of the previous row. If they are equal, we set it to $0$, otherwise we set it to $1$. Then, we use the window function `SUM` to calculate the prefix sum, which is the grouping identifier. Finally, we only need to group by the grouping identifier and filter out the numbers with a row count greater than or equal to $3$ in each group. Similarly, we need to use the `DISTINCT` keyword to remove duplicates from the results.
6367

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

6670
### **SQL**
6771

72+
```sql
73+
# Write your MySQL query statement below
74+
SELECT DISTINCT l2.num AS ConsecutiveNums
75+
FROM
76+
Logs AS l1
77+
JOIN Logs AS l2 ON l1.id = l2.id - 1 AND l1.num = l2.num
78+
JOIN Logs AS l3 ON l2.id = l3.id - 1 AND l2.num = l3.num;
79+
```
80+
6881
```sql
6982
# Write your MySQL query statement below
7083
WITH
7184
T AS (
7285
SELECT
7386
*,
74-
num = (LAG(num) OVER (ORDER BY id)) AS a,
75-
num = (LEAD(num) OVER (ORDER BY id)) AS b
87+
LAG(num) OVER (ORDER BY id) AS a,
88+
LEAD(num) OVER (ORDER BY id) AS b
7689
FROM Logs
7790
)
7891
SELECT DISTINCT num AS ConsecutiveNums
7992
FROM T
80-
WHERE a = 1 AND b = 1;
93+
WHERE a = num AND b = num;
8194
```
8295

8396
```sql

0 commit comments

Comments
 (0)