文章目录
1. 题目
2. 思路
(1) HashMap
- 首先利用HashMap记录每个分数所在的位置,然后对分数进行排序,最后将名次填到分数原来的位置上即可。
3. 代码
import java.util.*;
public class Test {
public static void main(String[] args) {
}
}
class Solution {
public String[] findRelativeRanks(int[] score) {
int n = score.length;
Map map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(score[i], i);
}
Arrays.sort(score);
String[] res = new String[n];
for (int i = 0; i < n; i++) {
int num = n - i;
int index = map.get(score[i]);
if (num > 3) {
res[index] = String.valueOf(num);
} else if (num == 3) {
res[index] = "Bronze Medal";
} else if (num == 2) {
res[index] = "Silver Medal";
} else {
res[index] = "Gold Medal";
}
}
return res;
}
}