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.


思路:

自己没看题解做出来的第一道middle难度的题目,这个题和Rasing Temprature很像,考察的都是一个表中不同元组之间的某些属性的比较

那个easy的题目只要比较连续的两行就好,而这个题目要比较三行

将easy题目的两种解法套用过来,其中一个会超时,及O(2n^2)时间复杂度的那个嵌套选择算法,我分析这种算法之所以超时是因为每一次子select都要遍历整张表来判断where a.Id = c.Id - 2和where a.Id = b.Id-1,而当表的数据非常大的时候,速度就会很慢

第二种AC了的算法,感觉是一种消耗空间节省时间的方法,我们建立了表格之后,只需要遍历一遍就可以得出最后的答案。


AC代码:
select distinct a.Num as consecutiveNums
from Logs as a,Logs as b,Logs as c
where a.Id = b.Id - 1 and a.Id = c.Id - 2 and a.Num = b.Num and a.Num = c.Num
TLE代码:
select distinct a.Num as ConsecutiveNums
from Logs as a
where a.Num = (
select b.Num
from Logs as b
where a.Id = b.Id -1
)
and
a.Num = (
select c.Num
from Logs as c
where a.Id = c.Id -2
)

leetcode-Consecutive numbers的更多相关文章

  1. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

  2. [LeetCode] Consecutive Numbers 连续的数字

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

  3. LeetCode——Consecutive Numbers

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

  4. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  5. LeetCode Database: Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  6. 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers

    题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...

  7. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  8. 【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...

  9. [LeetCode#180]Consecutive Numbers

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

  10. 829. Consecutive Numbers Sum

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

随机推荐

  1. 项目总结——深入浅出socket网络编程

    前言: 为什么会有如题的概念呢,我想对于没有主动听说过socket网络编程的人来说读到题目可能就已经蒙头了,为了很好的让大家进入场景,首先说一下一个需要用到这点东西的业务需求. 首先大家应该明确的是s ...

  2. 日积月累:ProguardGui进行jar包代码混淆

    前面文章<Proguard进行源代码混淆>讲解过怎么使用Proguard工具对Android的源代码进行混淆的方法(感兴趣的朋友可以访问:http://blog.csdn.net/p106 ...

  3. 重叠I/O之事件通知

      在 Winsock 中,重叠 I/O(Overlapped I/O)模型能达到更佳的系统性能,高于select模型.异步选择和事件选择三种.重叠模型的基本设计原理便是让应用程序使 用一个重叠的数据 ...

  4. android使用微软EWS发送邮件

    通常我们在android使用javamail发送邮件,可是很多时候我们需要连接Exchange服务(很多公司内部邮件服务器采用,并且未开通smtp服务)来发送邮件,这时候我们就要用到微软的 ews-j ...

  5. JAVA - hashcode与equals作用、关系

      Hashcode的作用 总的来说,Java中的集合(Collection)有两类,一类是List,再有一类是Set.前者集合内的元素是有序的,元素可以重复:后者元素无序,但元素不可重复.      ...

  6. Ubuntu 修改时区和时间

    tzselect #根据提示选择时区 sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #把设置好的时区文件粘贴到 /etc/local ...

  7. animation-timing-function中的cubic-bezier(n,n,n,n)

    cubic-bezier即为贝兹曲线中的绘制方法.图上有四点,P0-3,其中P0.P3是默认的点,对应了[0,0], [1,1].而剩下的P1.P2两点则是我们通过cubic-bezier()自定义的 ...

  8. python运行时间计算之timeit

    timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000) stmt:statement ...

  9. Python 中 open()文件操作的方式

    Python的open方法用来打开一个文件.第一个参数是文件的位置和文件名,第二个参数是读写模式: f=open('/1.txt','w') 读写模式的类型有: rU 或 Ua 以读方式打开, 同时提 ...

  10. deb文件安装命令

    一般在此类发行版中可以直接双击安装 手动安装.如果您喜欢使用终端,您需要管理员权限来安装一个 .deb 文件. 打开终端后,输入: sudo dpkg -i package_file.deb 要卸载一 ...