Similar approach to the 'find consecutive numbers' questions using user-defined variables

  • 1

    Modified the code from @baiji on another question about 'find consecutive numbers' and it was accepted.

    select Score,
    when @prevsc=Score then @n:=@n
    when (@prevsc:=Score) is not null then @n:=@n+1
    end Rank
    from Scores, (select @prevsc:=null, @n:=0)a
    order by Score desc

  • 0

    Do you actually write code like that, without any formatting, or did you just decide to torture your readers?

  • 1

    @ninikitten Thanks for your solution, I've run your code and found out that the Rank column turns out to be Strings rather than Int, have you met the same problem?

    The result is as following:
    {"headers": ["Score", "Rank"], "values": [[4.00, "1"], [4.00, "1"], [3.85, "2"], [3.65, "3"], [3.65, "3"], [3.50, "4"]]}

Log in to reply

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