180. Consecutive Numbers
来源:互联网 发布:腾讯算法面试题 编辑:程序博客网 时间:2024/06/11 03:37
题目:
找出连续出现大于或等于3次的数字。
表Logs
例如:1连续出现3次,结果如下。
解析
表内的联系,通常采用表的自连接。题目中主要涉及表内连续3次出现同一数字,那么就需要3张表做连接。
select distinct a.num as ConsecutiveNumsfrom Logs as ajoin Logs as b on a.id=b.id-1join Logs as c on a.id=c.id-2 where a.num=b.num and b.num=c.num
表a,b,c分别代表了第一次出现的数字,第二次出现的数字以及第三出现的数字。如果三次都相同,那么即为该数连续出现3次。此外,如果连续出现3次以上那么就会输出多个相同的满足条件的值,因此需要distinct
去重。
另一种解法为:
select distinct a.Num as ConsecutiveNums from Logs as a, Logs as b, Logs as cwhere a.Num = b.Num and b.Num = c.Num and a.Id=b.Id-1 and b.Id= c.Id-1
阅读全文
0 0
- leetcode 180. Consecutive Numbers
- leetcode 180. Consecutive Numbers
- Leetcode 180. Consecutive Numbers
- 180. Consecutive Numbers
- 180. Consecutive Numbers
- 180. Consecutive Numbers
- 180. Consecutive Numbers
- leetcode-database-180. Consecutive Numbers
- 【leetcode Database】180. Consecutive Numbers
- Consecutive Numbers
- Consecutive Numbers
- Consecutive Numbers
- Consecutive Numbers
- LEETCODE SQL题目 180. Consecutive Numbers
- leetcode 180. Consecutive Numbers 解题思路
- LeetCode | Consecutive Numbers
- [leetcode] Consecutive Numbers
- consecutive numbers 180
- Vue父子组件传值
- JOB:前端面试4
- android 6.0权限
- 鸡啄米C++编程
- HDU 1161 敌兵布阵 线段树单点更新
- 180. Consecutive Numbers
- ffmpeg将视频从隔行扫描转为逐行扫描
- DataTables整理
- python 元组数据
- Spring摘要
- Python性能优化
- throws和try-catch的使用场景
- 实体类忽略字段的序列化
- QT 之 TCP/IP 服务器和客户端(三)(数据流出流入操作版本)