leetcode-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.
思路:
自己没看题解做出来的第一道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的更多相关文章
- [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
1. 题目名称 Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- LeetCode——Consecutive Numbers
Description: Write a SQL query to find all numbers that appear at least three times consecutively. + ...
- 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. +----+ ...
- LeetCode Database: Consecutive Numbers
Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...
- 【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 ...
- [LeetCode] 829. Consecutive Numbers Sum 连续数字之和
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
- 【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...
- [LeetCode#180]Consecutive Numbers
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- 829. Consecutive Numbers Sum
Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...
随机推荐
- 使用glob()查找文件(转)
大部分PHP函数的函数名从字面上都可以理解其用途,但是当你看到 glob() 的时候,你也许并不知道这是用来做什么的,其实glob()和scandir() 一样,可以用来查找文件,请看下面的用法: ...
- NYOJ-569最大公约数之和
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=569 此题目可以用筛选法的思想来做,但是用到一个欧拉函数 gcd(1,12)=1,gcd( ...
- 新闻源图片放到js里
例子:http://www.s1979.com/jkys/20141209/2547965.html <script type="text/javascript" src=& ...
- Avi视频生成缩略图时,提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”
需求:录制Avi格式视频成功后,使用DirectShow生成缩略图,由于视频录制时,宽高分辨率可调节,所以有些情况下,生成缩略图会抛出异常“尝试读取或写入受保护的内存.这通常指示其他内存已损坏”. 异 ...
- QT 5.1.1 for Android 开发环境搭建与配置【Windows 7】
前言:本人操作系统为Windows7 64位,用的是32位的安装包,32位系统没有验证. 一.首先下载以下安装包,如果提供的链接失效请自行下载: (1) Android SDK (Windows 32 ...
- iOS开源 框架
UI界面类项目: Panoramagl ——720全景展示 Panorama viewer library foriPhone, iPad and iPod touch MBProgressHUD — ...
- zabbix log(logrt) key的使用
今天看了篇帖子是关于如何利用zabbix 自带的key去读log,监控linux异常登陆,一直以来都是自己写脚本去读log的.就想看看这个zabbix log 这个key怎么样..好吧开始: 官方文档 ...
- Linux(Centos、Debian)之安装Java JDK及注意事项(转)
--转自:http://www.cnblogs.com/hanyinglong/p/5025635.html 说明:本人是以Debian 操作系统来进行安装的,这篇文章有很大帮助,才学Linux对有些 ...
- MetInfo操作笔记
1.去版权(前台) 文件路径:templates/模板名称/foot.php <div class="powered_by_metinfo">Powered by &l ...
- CClientDC
CClientDC(客户区设备上下文)用于客户区的输出,它在构造函数中封装了GetDC(),在析构函数中封装了ReleaseDC()函数.一般在响应非窗口重画消息(如键盘输入时绘制文本.鼠标绘图)绘图 ...