My solution.......


  • 0
    H
    SELECT
        DISTINCT Num
    FROM (
        SELECT 
            CASE WHEN Num=@prevnum THEN @count:=@count+1 ELSE @count:=1 END,
            CASE WHEN @count>=3 THEN Num ELSE NULL END AS Num,
            @prevnum:=Num
        FROM Logs, (SELECT @prevnum:=NULL, @count:=0) AS A
    ) AS B
    WHERE Num IS NOT NULL

  • 0
    Y

    I have nearly the same code with you, but got "Time Limit Exceeded" when I use "distinct" first. Then I use "group by" to pass the problem. My code is:

    select Num ConsecutiveNums from (
    select case when Num=@prev and @count!=0 then @count:=@count+1
    else @count:=1
    end temp1,
    @prev:=Num temp2,
    @count count,
    Num
    from Logs, (select @count:=0)r1, (select @prev=-99999)r2
    ) t1 where count>=3 group by Num;


Log in to reply
 

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