506. Relative Ranks

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

    输入:[54321]

    输出:[“金牌”,“银牌”,“铜牌”,“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;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值