我承认,这题我投机取巧了,用了python自带的sorted排序,以及获取索引的方法,直接解出来了,还用上了字典,请大家批评指正。
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
sorted_score = sorted(score, reverse=True)
ranked_score = []
socre_dict = {0: "Gold Medal",
1: "Silver Medal",
2: "Bronze Medal",
}
for i in range(len(score)):
index = sorted_score.index(score[i])
if index < 3:
index = socre_dict[index]
else:
index = str(index + 1)
ranked_score.append(index)
return ranked_score