欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[mysql]leetcode180:连续出现的数字(medium)

程序员文章站 2022-03-13 23:33:50
...

题目:
[mysql]leetcode180:连续出现的数字(medium)
题解:

  • 题解1:由于只需要检索至少连续出现三次的数字,所以我们暴力求解即可,也就是id连续,且num相同。

题解1的代码如下:

# Write your MySQL query statement below
-- 解法1:暴力法,直接查询连续出现三次的数字,也就是id连续,且num相同
SELECT DISTINCT
    l1.Num AS ConsecutiveNums
FROM
    Logs l1,
    Logs l2,
    Logs l3
WHERE
    l1.Id = l2.Id - 1
    AND l2.Id = l3.Id - 1
    AND l1.Num = l2.Num
    AND l2.Num = l3.Num

题解2的代码如下:

SELECT DISTINCT num as ConsecutiveNums from(

	SELECT num,count(*) num_count FROM(
		SELECT id,num,
			row_number() over (ORDER BY id)-row_number() over (PARTITION by num order by id) as orde
			from logs
	)as w GROUP BY num,orde
	
)as s where num_count >=3