A simple solution using user defined variable

  • 0

    this is my solution using user defined variables

    select distinct Num as ConsecutiveNums from(
        select Num,
            @count := if(@prev = Num,@count+1,1) as count,
            @prev := Num as dummy
       from Logs,(select @count:=0,@prev:=(select Num from logs limit 1)) tmp
    ) T where T.count >= 3;

    Table T contain three columns Num and count [which is the number of previous consecutive occurrence of Num] which is @count
    and the other variable is @prev contain the previous value of Num and it's initial value is the first one in the Logs table

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.