当前位置:网站首页>Leetcode day2 连续出现的数字
Leetcode day2 连续出现的数字
2022-07-28 17:47:00 【wyqgg123】
180. 连续出现的数字
难度中等
SQL架构
表:Logs
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| num | varchar |
+-------------+---------+
id 是这个表的主键。
编写一个 SQL 查询,查找所有至少连续出现三次的数字。
返回的结果表中的数据可以按 任意顺序 排列。
查询结果格式如下面的例子所示:
Logs 表:
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
Result 表:
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
1 是唯一连续出现至少三次的数字。
解法一:
通过自表连接找到当前id的下一个和下下一个进行连接,然后通过条件判断获取当前的num等于下面连这两个的num的num值,然后进行去重操作,这样就获得了连续出现三次的num,这种写法没有考虑id不连续,以及若出现n次连续的情况,可用性不高
代码示例:
select distinct a.Num as ConsecutiveNums from logs as a
left join logs as b on a.id = b.id+1
left join logs as c on a.id = c.id+2
where a.Num = b.Num and a.Num = c.Num;
解法二:
1、首先考虑若id不连续的情况出现的话我们就需要多查询一列来构造连续的一列,这里可以用到窗口函数 row_number()
select *,row_number() over (order by id) as new_id from Logs;
这样可以实现若id不连续,通过查询构建一个连续的列new_id,具体实现如下表
| id | num | new_id |
|---|---|---|
| 1 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 3 |
| 7 | 3 | 4 |
| 8 | 3 | 5 |
| 9 | 2 | 6 |
2、然后在通过分组查询将num一致的列分组排序查询
select *,row_number over (partition by Num order by id) as new_order from Logs;
运行结果如下表
| id | num | new_order |
|---|---|---|
| 1 | 2 | 1 |
| 3 | 2 | 2 |
| 9 | 2 | 3 |
| 4 | 3 | 1 |
| 7 | 3 | 2 |
| 8 | 3 | 3 |
sql示例:
select *,row_number() over(order by id) as new_id,
row_number() over (partition by Num order by id) as new_order
from Logs;
运行结果如下表:
| id | num | new_id | new_order |
|---|---|---|---|
| 1 | 2 | 1 | 1 |
| 3 | 2 | 2 | 2 |
| 9 | 2 | 6 | 3 |
| 4 | 3 | 3 | 1 |
| 7 | 3 | 4 | 2 |
| 8 | 3 | 5 | 3 |
将这两次查询联合起来我们可以发现规律:
用new_id减去new_order的值只要相同,那么他们就是连续的。也就是:如果一个num连续出现时,那么它出现的[真实序列]-它出现的次数一定是个定值。解题思路:第一步就是首先获取他出现的真实序列,第二步就是获取它出现的次数,然后下一步就是它们之间相减,减完之后若值相同,那么他们一定是连续的。
3、[真实序列]-它出现的次数
select *,
row_number() over(order by id) - row_number() over (partition by Num order by id) as sta,
from Logs;
运行结果表:
| id | num | sta |
|---|---|---|
| 1 | 2 | 0 |
| 3 | 2 | 0 |
| 9 | 2 | 3 |
| 4 | 3 | 2 |
| 7 | 3 | 2 |
| 8 | 3 | 2 |
可以看出有两个连续的数,一个是id为1、3的两个连续,另一个是4、7、8三个连续,根据数据可以看出没有问题,那么我们就可以通过这个来找出连续出现n次的数据。
select distinct Num as ConsecutiveNums
from
(
select id,Num,
row_number() over (order by id) - row_number() over (partition by Num order by id) as sta from Logs
) as Sub group by Num , sta having count(1)>=3
在这里 查询 子查询查询出来的结果集,通过Num分组,连续出现的次数如果超过三次则将改Num查询出来,并进行去重操作,若要查询出现n次的数据只需要将having条件中的count(1)条件改为大于n即可。
运行结果:
ConsecutiveNums : 1
总结:
第一种解法比较容易理解和实现,但是具有一定的局限性。
第二种解法通过规律解题,实现相比来说比较复杂,但是实用性更强。
边栏推荐
- Convertible bond concept table x notation gives you a convenient and fast experience!
- 使用SaltStack自动化部署Zabbix
- Force buckle 1331. Array serial number conversion
- SaltStack之salt-ssh
- Rust 入门指南(rustup, cargo)
- Search problems and technologies
- 芯片功耗性能验证:从困境到超越
- MySQL8 Status Variables: Internal Temporary Tables and Files
- New this prototype precompiled exercise
- Taking the opportunity of digital transformation, how can 3C enterprises achieve efficient collaboration through SRM supplier cloud collaboration platform?
猜你喜欢

Smart contract security - overflow vulnerability

文章翻译软件-批量免费翻译软件支持各大翻译接口

Android-第十三节03xUtils-数据库框架(增删改查)详解

App自动化测试是怎么实现H5测试的

彻底理解位运算——左移、右移

微信公众号授权登录后报redirect_uri参数错误的问题

使用SaltStack自动化部署LNMP

Transformer for anomaly detection - instra "painting transformer for anomaly detection"

毕马威中国:证券基金经营机构信息技术审计项目发现洞察

Iclr21 (classification) - future classic "vit" an image is worth 16x16 words (including code analysis)
随机推荐
Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
Using Baidu easydl to realize chef hat recognition of bright kitchen and stove
【已解决】AC86U ML改版固件虚拟内存创建失败,提示USB磁盘读写速度不满足要求
ES6 new - arrow function
开盘暴涨215%!国产信号链芯片企业芯海科技登陆科创板
WPF 实现带蒙版的 MessageBox 消息提示框
Jestson nano Object detection
Germany and Portugal have announced that they will not disable Huawei 5g equipment, but Germany will set strict restrictions!
redis 主从架构(sizeof函数怎么计算)
Doxygen document generation tool
OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
第一次写博客
After reading the thesis for three years, I learned to read the abstract today
MySQL8 Status Variables: Internal Temporary Tables and Files
andorid系统layout、values、drawable适配
First blog
峰值速率超2Gbps!高通在中国首发通过5G毫米波MIMO OTA测试
在矩池云快速安装torch-sparse、torch-geometric等包
SaltStack系统初始化
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作