""" 相对名次 """ from typing import List class Solution: def findRelativeRanks(self, score: List[int]) -> List[str]: """ 列表排序,并倒序,使用字典存储起来,再一个一个的去找 :param score: :return: """ res = [] dict1 = {} new_nums = sorted(score, reverse=True) for index, num in enumerate(new_nums): dict1[num] = index + 1 for s in score: if dict1[s] == 1: res.append('Gold Medal') elif dict1[s] == 2: res.append('Silver Medal') elif dict1[s] == 3: res.append('Bronze Medal') else: res.append(str(dict1[s])) return res if __name__ == '__main__': result = Solution().findRelativeRanks([5, 4, 3, 2, 1]) print(result)