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. ORACLE函数之单行数字函数

     1.           ABS(X) 返回X的绝对值 SQL>SELECT ABS(-1) A,ABS(1) B,ABS(0) C FROM DUAL; A          B     ...

  2. table标签

    table标签使我们最常用的的标签,在使用table标签时我们要注意一些其属性,早期我们经常使用table标签对其进行页面布局但是现在我们基本不再使用,由此可见table标签也是非常强大的一个工具. ...

  3. linux-swappiness参数的作用及设置

    linux 会使用硬盘的一部分做为SWAP分区,用来进行进程调度--进程是正在运行的程序--把当前不用的进程调成‘等待(standby)‘,甚至‘睡眠(sleep)’,一旦要用,再调成‘活动(acti ...

  4. CentOS 7 安装教程

    参考资料: http://www.cnblogs.com/bobbylinux/articles/centos7.html

  5. 图片样式 scaleType 属性

    ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType),不同值的意义区别:

  6. JY01-KX-01

    复习: 1.a标签跳转 <p id="地址"></p> <a href="#地址"></a> 预习: 1.out ...

  7. .net面试总结

    一. hr 为人处事 工作中遇到问题:沟通很重要 离职原因:公司倒闭 二. ISAPI Internet Server Application Program Interface 三. http状态码 ...

  8. sql查询语句心得

    1)where 有多个用in ,一个用= 2)自身链接 select A.Sno from S A,S B where A.Sname='a' AND A.City=B.City)) 3)外链接(同时 ...

  9. HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)

    以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...

  10. ACM中常用的C/C++函数

    只大概说明功能,具体用法请自行百度. C函数 memset:按字节填充地址空间 sscanf:从一个字符串中格式化读取变量 sprintf:将变量格式化写入字符串中 atoi:字符串转int atof ...