题目
代码部分(22ms 55.88%)
class Solution {
public String[] findRelativeRanks(int[] nums) {
if(nums.length == 0) return new String[0];
if(nums.length == 1) {
String[] s = new String[1];
s[0] = "Gold Medal";
return s;
}
if(nums.length == 2) {
String[] s = new String[2];
if(nums[0] > nums[1]){
s[0] = "Gold Medal";
s[1] = "Silver Medal";
}else{
s[1] = "Gold Medal";
s[0] = "Silver Medal";
}
return s;
}
int len = nums.length;
String [] res = new String[len];
HashMap<Integer,Integer> map = new HashMap();
for(int i = 0; i < len; i++){
map.put(nums[i], i);
}
Arrays.sort(nums);
int j = len - 3;
int first = map.get(nums[j + 2]);
int second = map.get(nums[j + 1]);
int third = map.get(nums[j]);
res[first] = "Gold Medal";
res[second] = "Silver Medal";
res[third] = "Bronze Medal";
int n = 4;
for(int i = j - 1; i >= 0; i--){
int index = map.get(nums[i]);
res[index] = "" + (n++);
}
return res;
}
}