Given scores of N athletes,find their relative ranks and the people with the top three highest scores, whowill be awarded medals: "Gold Medal", "Silver Medal" and"Bronze Medal".
Example 1:
Input: [5, 4, 3, 2, 1]
Output: ["GoldMedal", "Silver Medal", "Bronze Medal", "4","5"]
Explanation: The first threeathletes got the top three highest scores, so they got "Gold Medal","Silver Medal" and "Bronze Medal".
For the left two athletes, youjust need to output their relative ranks according to their scores.
Note:
N is a positive integer andwon't exceed 10,000.
All the scores of athletes areguaranteed to be unique.
谷歌翻译: 考虑到N个运动员的得分,找到他们的相对等级和具有最高三分的人,他们将被授予奖牌:“金牌”,“银牌”和“铜牌”。
实施例1:
输入:[5,4,3,2,1]
输出:[“金牌”,“银牌”,“铜牌”,“4”,“5”]
说明:前三名运动员获得了前三名,获得“金牌”,“银牌”和“铜牌”。
对于左边的两个运动员,你只需要根据他们的分数输出他们的相对排名。
注意:N是正整数,不超过10,000。所有的运动员得分都是独一无二的。
这题开始以为是数字的大小不会超过10000,又不考虑时间复杂度,所以用了桶排序。测试几组数据是对的,提交时最后发现数组越界。自己把题目意思搞错了。看的参考,也供大家参考一下吧。写完这个,我会找一些关于hashMap和TreeMap的区别与联系,并且仔细的写一篇关于Map集合的文章。代码如下:
public class Solution {
public String[] findRelativeRanks(int[]nums) {
int rank[]=nums.clone();
Arrays.sort(rank);//从小到大的顺序
Map<Integer,Integer> map=new HashMap();
String[] str=new String[nums.length];
for(int i=0;i<rank.length;i++)
map.put(rank[i], nums.length-i);
for(int i=0;i<nums.length;i++){
int k=map.get(nums[i]);
String res=k+"";
if(k==1) res= "Gold Medal";
else if(k==2)res = "Silver Medal";
else if(k==3)res = "Bronze Medal";
str[i]=res;
}
return str;
}
}