Leetcode刷题100天—506. 相对名次(哈希)—day77

前言:

作者:神的孩子在歌唱

大家好,我叫智

image-20211202212206616

506. 相对名次

难度简单151

给你一个长度为 n 的整数数组 score ,其中 score[i] 是第 i 位运动员在比赛中的得分。所有得分都 互不相同

运动员将根据得分 决定名次 ,其中名次第 1 的运动员得分最高,名次第 2 的运动员得分第 2 高,依此类推。运动员的名次决定了他们的获奖情况:

  • 名次第 1 的运动员获金牌 "Gold Medal"
  • 名次第 2 的运动员获银牌 "Silver Medal"
  • 名次第 3 的运动员获铜牌 "Bronze Medal"
  • 从名次第 4 到第 n 的运动员,只能获得他们的名次编号(即,名次第 x 的运动员获得编号 "x")。

使用长度为 n 的数组 answer 返回获奖,其中 answer[i] 是第 i 位运动员的获奖情况。

示例 1:

输入:score = [5,4,3,2,1]
输出:["Gold Medal","Silver Medal","Bronze Medal","4","5"]
解释:名次为 [1st, 2nd, 3rd, 4th, 5th] 。

示例 2:

输入:score = [10,3,8,9,4]
输出:["Gold Medal","5","Bronze Medal","Silver Medal","4"]
解释:名次为 [1st, 5th, 3rd, 2nd, 4th] 。

提示:

  • n == score.length
  • 1 <= n <= 104
  • 0 <= score[i] <= 106
  • score 中的所有值 互不相同
class Solution {
    
    
    public String[] findRelativeRanks(int[] score) {
    
    
        int len=score.length;
        String[] src={
    
    "Gold Medal","Silver Medal","Bronze Medal"};
//        存入hashMap
        HashMap<Integer,Integer> hashMap=new HashMap<>();
        for (int i=0;i<len;i++){
    
    
            hashMap.put(score[i],i);
        }
//        排序
        Arrays.sort(score);
//        定义字符数组
        String[] strings=new String[len];
        int len1=0;
        for (int i=len-1;i>=0;i--){
    
    
//            通过哈希获取排序前的下标
            
            if (len1<3){
    
    
                // System.out.println(score[i]);
                strings[hashMap.get(score[i])]=src[len1++];
            }else{
    
    
                strings[hashMap.get(score[i])]=String.valueOf(++len1);
            }
        }
        return strings;
    }
}

本人csdn博客:https://blog.csdn.net/weixin_46654114

转载说明:跟我说明,务必注明来源,附带本人博客连接。

猜你喜欢

转载自blog.csdn.net/weixin_46654114/article/details/122973055