Skip to content

Commit 6699acf

Browse files
committed
Add solution 180 [sql]
Consecutive Numbers
1 parent 452d05b commit 6699acf

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
8989
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
9090
| 177 | [Nth Highest Salary](https://github.com/doocs/leetcode/tree/master/solution/177.Nth%20Highest%20Salary) | `SQL` |
9191
| 178 | [Rank Scores](https://github.com/doocs/leetcode/tree/master/solution/178.Rank%20Scores) | `SQL` |
92+
| 180 | [Consecutive Numbers](https://github.com/doocs/leetcode/tree/master/solution/180.Consecutive%20Numbers) | `SQL` |
9293
| 328 | [Odd Even Linked List](https://github.com/doocs/leetcode/tree/master/solution/328.Odd%20Even%20Linked%20List) | `Linked List` |
9394

9495

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## 连续出现的数字
2+
### 题目描述
3+
4+
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
5+
```
6+
+----+-----+
7+
| Id | Num |
8+
+----+-----+
9+
| 1 | 1 |
10+
| 2 | 1 |
11+
| 3 | 1 |
12+
| 4 | 2 |
13+
| 5 | 1 |
14+
| 6 | 2 |
15+
| 7 | 2 |
16+
+----+-----+
17+
```
18+
19+
例如,给定上面的 `Logs` 表, `1` 是唯一连续出现至少三次的数字。
20+
```
21+
+-----------------+
22+
| ConsecutiveNums |
23+
+-----------------+
24+
| 1 |
25+
+-----------------+
26+
```
27+
28+
### 解法
29+
联表查询。
30+
31+
```sql
32+
# Write your MySQL query statement below
33+
34+
select distinct l1.Num ConsecutiveNums
35+
from Logs l1
36+
join Logs l2 on l1.Id = l2.Id - 1
37+
join Logs l3 on l2.Id = l3.Id - 1
38+
where l1.Num = l2.Num and l2.Num = l3.Num
39+
40+
41+
```
42+
43+
#### Input
44+
```json
45+
{"headers": {"Logs": ["Id", "Num"]}, "rows": {"Logs": [[1, 1], [2, 1], [3, 1], [4, 2], [5, 1], [6, 2], [7, 2]]}}
46+
```
47+
48+
#### Output
49+
```json
50+
{"headers":["ConsecutiveNums"],"values":[[1]]}
51+
```

0 commit comments

Comments
 (0)