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-16 14:00
白火同学:其实你可以了解一下HR在Boss聊天的机制,想赢牌的前提是先会玩牌。 如果HR长时间没有理你,有可能是因为你的消息被其他应聘者的消息给挤到下面了,HR从上到下有可能只看个三四百个人就要到理想数量的简历了,而你恰好没有被看到,时间一长,你的消息在越来越下面。这种情况就需要你自己活跃一下,把消息提上去。 也可能是HR招的合适的人选了,但会一直挂着岗位,为了省重新开招聘岗位的钱,方便后面随时修改招聘要求。 当然也可能是HR吃饱了没事耍你玩,要了你的简历又不看,就看你自己怎么理解了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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