当前位置:网站首页>Leetcode Day2 consecutive numbers
Leetcode Day2 consecutive numbers
2022-07-28 19:43:00 【wyqgg123】
180. Consecutive numbers
Medium difficulty
SQL framework
surface :Logs
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| num | varchar |
+-------------+---------+
id It's the primary key of this table .
Write a SQL Inquire about , Find all numbers that appear at least three times in a row .
The data in the returned result table can be displayed by In any order array .
The query result format is shown in the following example :
Logs surface :
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
Result surface :
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
1 Is the only number that appears at least three times in a row .
Solution 1 :
Find the current through self table connection id Connect the next one of with the next one , Then get the current through condition judgment num Equal to the following two num Of num value , Then perform the de duplication operation , In this way, three consecutive occurrences of num, This way of writing does not consider id Discontinuous , And if n Sub continuous situation , Low availability
Code example :
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;
Solution 2 :
1、 First consider if id In case of discontinuity, we need to query one more column to construct a continuous column , Here you can use the window function row_number()
select *,row_number() over (order by id) as new_id from Logs;
In this way, if id Discontinuous , Build a continuous column by querying new_id, The specific implementation is shown in the following table
| id | num | new_id |
|---|---|---|
| 1 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 3 |
| 7 | 3 | 4 |
| 8 | 3 | 5 |
| 9 | 2 | 6 |
2、 Then through grouping query num Consistent column grouping Sorting Query
select *,row_number over (partition by Num order by id) as new_order from Logs;
The operation results are shown in the following table
| id | num | new_order |
|---|---|---|
| 1 | 2 | 1 |
| 3 | 2 | 2 |
| 9 | 2 | 3 |
| 4 | 3 | 1 |
| 7 | 3 | 2 |
| 8 | 3 | 3 |
sql Example :
select *,row_number() over(order by id) as new_id,
row_number() over (partition by Num order by id) as new_order
from Logs;
The operation results are shown in the following table :
| 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 |
Combining these two queries, we can find the rule :
use new_id subtract new_order As long as the value of is the same , Then they are continuous . That is to say : If one num Continuous occurrence , Then it appears [ Real sequence ]- The number of times it appears must be a fixed value . Their thinking : The first step is to get the real sequence of his appearance , The second step is to get the number of times it appears , Then the next step is to subtract them , If the value is the same after subtraction , Then they must be continuous .
3、[ Real sequence ]- The number of times it appears
select *,
row_number() over(order by id) - row_number() over (partition by Num order by id) as sta,
from Logs;
Operation result table :
| id | num | sta |
|---|---|---|
| 1 | 2 | 0 |
| 3 | 2 | 0 |
| 9 | 2 | 3 |
| 4 | 3 | 2 |
| 7 | 3 | 2 |
| 8 | 3 | 2 |
It can be seen that there are two consecutive numbers , One is id by 1、3 Two consecutive , The other is 4、7、8 Three in a row , According to the data, there is no problem , Then we can find out the continuous occurrence through this n Secondary data .
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
ad locum Inquire about The result set of subquery , adopt Num grouping , If the number of consecutive occurrences exceeds three, it will be changed Num Find out , And carry out the de duplication operation , To query n The secondary data only needs to be having In the condition of count(1) The condition is changed to be greater than n that will do .
Running results :
ConsecutiveNums : 1
summary :
The first solution is easy to understand and realize , But there are some limitations .
The second solution is to solve problems through laws , The implementation is relatively complex , But it's more practical .
边栏推荐
- Android-第十三节03xUtils-数据库框架(增删改查)详解
- 亚马逊推出Amazon One手掌支付系统,非接触式掌静脉识别市场有望爆发
- lua语言的左对齐函数(手写)
- MySQL8 tmp_ table_ Size and Max_ heap_ table_ size
- 冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题
- CodeIgnier框架实现restful API接口编程
- redis 主从架构(sizeof函数怎么计算)
- MySQL8 Status Variables: Internal Temporary Tables and Files
- High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
- NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
猜你喜欢

WPF implements MessageBox message prompt box with mask

Rust Getting Started Guide (crite Management)

Sword finger offer II 109. unlock the password lock

你知道雨的类型有几种?

Force buckle 1331. Array serial number conversion

认识中小型局域网WLAN

基于QTGUI图像界面的空战游戏设计

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

Saltstack system initialization

adb remount of the / superblock failed: Permission denied
随机推荐
WPF implements MessageBox message prompt box with mask
个人博克系统登录点击图形验证码的集成与实现
Cvpr21 unsupervised anomaly detection cutpaste:self supervised learning for anomaly detection and localization
In order to develop high-end photoresist, Jingrui Co., Ltd. invested 75million yuan to purchase SK Hynix ASML lithography machine
微信公众号授权登录后报redirect_uri参数错误的问题
Smart contract security - overflow vulnerability
editor.md中markdown编辑器的实现
为研发高端光刻胶,晶瑞股份斥资7500万元购买SK海力士的ASML光刻机
MySQL performance testing tool sysbench learning
【微信小程序开发】页面导航与传参
宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0
博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)
Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
navicate修改数据库名的方式
美国将提供250亿美元补贴,鼓励英特尔等芯片制造商迁回产线
Android-第十三节03xUtils-数据库框架(增删改查)详解
andorid系统layout、values、drawable适配
华为入股南京芯视界,布局固态激光雷达芯片领域
App自动化测试是怎么实现H5测试的
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)