DDSA Solutions

180. Consecutive Numbers

Advertisement

About this solution

Consecutive Numbers is a unknown-difficulty LeetCode problem covering the Database pattern. The MySQL solution below uses an idiomatic approach that is clean, readable, and directly submittable on LeetCode. Study the logic carefully — recognising the underlying pattern is the key skill that transfers to similar problems in interviews.

Key Techniques

180.sql
MySQL
/* Write your T-SQL query statement below */
WITH
  LogsNeighbors AS (
    SELECT
      *,
      LAG(num) OVER(ORDER BY id) AS prev_num,
      LEAD(num) OVER(ORDER BY id) AS next_num
    FROM LOGS
  )
SELECT DISTINCT num AS ConsecutiveNums
FROM LogsNeighbors
WHERE
  num = prev_num
  AND num = next_num;
Advertisement
Was this solution helpful?