LeetCode: 180. Consecutive Numbers

LeetCode: 180. Consecutive Numbers

题目描述

Write a SQL query to find all numbers that appear at least three times consecutively.

+----+-----+
| Id | Num | +----+-----+
| 1  |  1  |
| 2  |  1  |
| 3  |  1  |
| 4  |  2  |
| 5  |  1  |
| 6  |  2  |
| 7 | 2 | +----+-----+

For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.

+-----------------+
| ConsecutiveNums | +-----------------+
| 1 | +-----------------+

解题思路

将三个 logs 表链接,然后筛选出符合要求的内容。

AC 代码

SELECT DISTINCT logs1.num AS ConsecutiveNums 
FROM logs AS logs1, logs AS logs2, logs As logs3
WHERE logs1.Id = logs2.Id+1 AND logs2.Id = logs3.Id+1 AND logs1.Num = logs2.Num AND logs2.Num = logs3.Num
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务