1.题目
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”(”Gold Medal”, “Silver Medal”, “Bronze Medal”)。
(注:分数越高的选手,排名越靠前。)
示例:
输入: [5, 4, 3, 2, 1]输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
提示:
- N 是一个正整数并且不会超过 10000。
- 所有运动员的成绩都不相同。
2.思路
利用排序+map
public String[] findRelativeRanks(int[] nums) {Map<Integer, Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {map.put(nums[i], i);}Arrays.sort(nums);String[] strings = new String[nums.length];for (int i = nums.length - 1; i >=0 ; i--) {if (i == nums.length - 1) {strings[map.get(nums[i])] = "Gold Medal";} else if (i == nums.length - 2) {strings[map.get(nums[i])] = "Silver Medal";} else if (i == nums.length - 3) {strings[map.get(nums[i])] = "Bronze Medal";} else {strings[map.get(nums[i])] = String.valueOf(nums.length - i );}}return strings;}
