File tree 3 files changed +37
-0
lines changed
solutions/178.Rank_Scores
3 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ SELECT A .Score Score,
2
+ (
3
+ SELECT COUNT (DISTINCT B .Score )
4
+ FROM Scores B
5
+ WHERE B .Score >= A .Score
6
+ ) Rank
7
+ FROM Scores A
8
+ ORDER BY A .Score DESC
9
+
Original file line number Diff line number Diff line change
1
+ SELECT Score,
2
+ (
3
+ CASE
4
+ # if prev = curRow.Score then return rank
5
+ WHEN @prev = Score THEN @rank
6
+ # else let prev = curRow.Score, rank++, and return rank
7
+ WHEN (@prev := Score) IS NOT NULL THEN @rank := @rank + 1
8
+ END
9
+ )
10
+ FROM Scores,
11
+ # SELECT to initialize rank, prev. The 'a': Every derived table must have its own alias
12
+ (SELECT @rank := 0 , @prev := NULL ) a
13
+ ORDER BY Score DESC
14
+
Original file line number Diff line number Diff line change
1
+ ## 178. Rank Scores (Medium)
2
+
3
+ ### ** 链接** :
4
+ 题目:https://leetcode.com/problems/rank-scores/
5
+ 代码(github):https://github.com/illuz/leetcode
6
+
7
+ ### ** 题意** :
8
+ 给分数排名。
9
+
10
+ ### ** 分析** :
11
+
12
+ 1 . 把 SELECT 套进去。
13
+ 2 . 用 CASE 和 变量,逐行处理,速度比 1 变了很多。
14
+
You can’t perform that action at this time.
0 commit comments