zoukankan      html  css  js  c++  java
  • Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

    Example 1:

    Input: [5, 4, 3, 2, 1]
    Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
    Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
    For the left two athletes, you just need to output their relative ranks according to their scores.

    Note:

    1. N is a positive integer and won't exceed 10,000.
    2. All the scores of athletes are guaranteed to be unique.
     1 public class Solution {
     2     public String[] findRelativeRanks(int[] nums) {
     3         int n = nums.length;
     4         
     5         HashMap map = new HashMap();
     6         for (int i = 0; i < n; i++) {
     7             map.put(nums[i], i);
     8         }
     9         
    10         Arrays.sort(nums);
    11         String[] result = new String[n];
    12         
    13         for (int i = 0; i < n; i++) {
    14             int index = (int) map.get(nums[i]);
    15             
    16             if (i == n - 1) result[index] = "Gold Medal";
    17             else if (i == n - 2) result[index] = "Silver Medal";
    18             else if (i == n - 3) result[index] = "Bronze Medal";
    19             else result[index] = Integer.toString(n - i);
    20         }
    21         return result;
    22     }
    23 }
  • 相关阅读:
    itextpdf 和 pdfbox 测试用例
    Java base64 编码、解码
    常用字体
    DIY 单片机 入门
    太难了。
    Java 之 native:JNI 本地方法测试实现
    Java 之 String 引用传递
    Java class 文件 方法的指令含义
    Spring学习文章链接
    数据结构_树_图_总结
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6391364.html
Copyright © 2011-2022 走看看