【Leetcode】Consecutive Numbers
2016-05-16 11:11
239 查看
题目链接:https://leetcode.com/problems/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.
思路:
三表联查,注意多表联合join的用法
算法:
题目:
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.
思路:
三表联查,注意多表联合join的用法
算法:
select distinct l1.num from (Logs l1 inner join Logs l2 on l1.num=l2.num and l1.id+1=l2.id) inner join Logs l3 on l1.num=l3.num and l1.id+2=l3.id
相关文章推荐
- vi编辑只读文档无法保存的解决办法
- python 无法使用方向键退格键
- c# 使用递归 循环遍历导航树结构 并解析
- css垂直居中方法(四)
- linux内核代码container_of
- Swift 中关于”??”操作符
- Box2d源码学习<一>之Box2d简介
- Android控件的两次及多次点击事件
- linux下如何查看某个软件 是否安装??? 安装路径在哪???
- ARM平台基于嵌入式Linux部署ROS
- CentOS 下JDK安装
- Servlet 中文乱码问题及解决方案剖析
- 【原创】C#模拟Post请求,正文为json数据的代码参考
- python中的break和continue
- Android ActionBar使用教程
- 缓存穿透、缓存并发、缓存失效之思路变迁
- 机器学习中分类评估方法简介 - 1
- 梯度下降法和反向传导法
- HDU2328 Corporate Identity
- iOS 获取手机验证码