给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”(”Gold Medal”, “Silver Medal”, “Bronze Medal”)

    (注:分数越高的选手,排名越靠前。)

    示例 1:

    输入: [5, 4, 3, 2, 1] 输出: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”] 解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” (“Gold Medal”, “Silver Medal” and “Bronze Medal”). 余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。

    提示:

    1. N 是一个正整数并且不会超过 10000。
    2. 所有运动员的成绩都不相同。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/relative-ranks


    1. class Solution {
    2. public String[] findRelativeRanks(int[] score) {
    3. String[] result = new String[score.length];
    4. // 定义一个change 用于将每次找到的成绩最大值都换成改值避免重复
    5. int change = Integer.MIN_VALUE;
    6. for (int i = 0; i < score.length; i++) {
    7. int maxScoreIndex = findMaxScoreIndex(score);
    8. score[maxScoreIndex] = change;
    9. if (i == 0){
    10. result[maxScoreIndex] = "Gold Medal";
    11. }else if (i == 1){
    12. result[maxScoreIndex] = "Silver Medal";
    13. }else if (i == 2){
    14. result[maxScoreIndex] = "Bronze Medal";
    15. }else {
    16. result[maxScoreIndex] = String.valueOf(i + 1);
    17. }
    18. }
    19. return result;
    20. }
    21. // 查找成绩最大值的下标
    22. public int findMaxScoreIndex(int[] score){
    23. int max = Integer.MIN_VALUE;
    24. int index = 0;
    25. for (int i = 0; i < score.length; i++) {
    26. if (score[i] > max){
    27. max = score[i];
    28. index = i;
    29. }
    30. }
    31. return index;
    32. }
    33. }