#107. [LeetCode / MySQL] 180. Consecutive Numbers

문제

 

입력 코드

1. 

SELECT DISTINCT a.num AS ConsecutiveNums
FROM Logs AS a
JOIN Logs AS b
    ON (a.id = b.id + 1) AND (a.num = b.num)
JOIN Logs AS c
    ON (b.id = c.id + 1) AND (b.num = c.num)

 

2. 

WITH cte AS (
    SELECT
        num,
        LAG(num) OVER (ORDER BY id) AS prev_num,
        LEAD(num) OVER (ORDER BY id) AS next_num
    FROM Logs
)

SELECT num AS ConsecutiveNums
FROM cte
WHERE num = prev_num AND num = next_num
GROUP BY num;

 

코드 설명

#DISTINCT #JOIN #SELF JOIN #WITH #LAG #LEAD #ORDER BY #GROUP BY 

 

 

문제 출처

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com