首页 > 其他 > 详细

Relative Ranks

时间:2017-02-12 18:58:27      阅读:399      评论:0      收藏:0      [点我收藏+]

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 }

 

Relative Ranks

原文:http://www.cnblogs.com/amazingzoe/p/6391364.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!