Skip to content

Latest commit

 

History

History
26 lines (23 loc) · 809 Bytes

506.相对名次.md

File metadata and controls

26 lines (23 loc) · 809 Bytes

506.相对名次

原题

把每个人的分数和下标写入二元组,再根据分数排序即可

class Solution:
    def findRelativeRanks(self, score: List[int]) -> List[str]:
        n = len(score)
        tmpS = []
        for i in range(n):
            tmpS.append((score[i], i))
        tmpS.sort(key = lambda x : -x[0])
        res = [0 ] * n		#最终结果。放0填充
        for i in range(n):
            if i == 0:	#第一名
                res[tmpS[i][1]] = "Gold Medal"
            elif i == 1:	#第二名
                res[tmpS[i][1]] = "Silver Medal"
            elif i == 2:	#第三名
                res[tmpS[i][1]] = "Bronze Medal"
            else:
                res[tmpS[i][1]] = str(i+1)
        return res