天天看點

​LeetCode刷題實戰178:分數排名

算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業務邏輯面試+算法面試。是以,為了提高大家的算法能力,這個公衆号後續每天帶大家做一道算法題,題目就從LeetCode上面選 !

今天和大家聊的問題叫做 分數排名  ,我們先來看題面:

https://leetcode-cn.com/problems/rank-scores/

Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.

題意

編寫一個 SQL 查詢來實作分數排名。

​LeetCode刷題實戰178:分數排名

解題

思路:排名問題直接可以用相關的視窗函數,由于Rank為關鍵字,是以必須加上反引号 。

SELECT score,
dense_rank() over(ORDER BY score DESC) as `Rank`
FROM scores;
           

好了,今天的文章就到這裡,如果覺得有所收獲,請順手點個在看或者轉發吧,你們的支援是我最大的動力 。

上期推文:

LeetCode1-160題彙總,希望對你有點幫助!

LeetCode刷題實戰161:相隔為1的編輯距離

LeetCode刷題實戰162:尋找峰值

LeetCode刷題實戰163:缺失的區間

LeetCode刷題實戰164:最大間距

LeetCode刷題實戰165:比較版本号

LeetCode刷題實戰166:分數到小數

LeetCode刷題實戰167:兩數之和 II - 輸入有序數組

LeetCode刷題實戰168:Excel表列名稱

LeetCode刷題實戰169:多數元素

LeetCode刷題實戰170:兩數之和 III - 資料結構設計

LeetCode刷題實戰171:Excel表列序号

LeetCode刷題實戰172:階乘後的零

LeetCode刷題實戰173:二叉搜尋樹疊代器

LeetCode刷題實戰174:地下城遊戲

LeetCode刷題實戰175:組合兩個表

LeetCode刷題實戰176:第二高的薪水

LeetCode刷題實戰177:第N高的薪水

​LeetCode刷題實戰178:分數排名

繼續閱讀