/*****************************************************问题描述*************************************************
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:
N is a positive integer and won't exceed 10,000.
All the scores of athletes are guaranteed to be unique.
给定n个运动员的分数,返回他们的名次,前3名名次分别是金、银、铜牌.
/*****************************************************我的解答*************************************************
/**
* @param {number[]} nums
* @return {string[]}
*/
var findRelativeRanks = function(nums) {
var arrayCopy = [];
for(var index = 0; index < nums.length; index++)
{
arrayCopy.push(nums[index]);
}
arrayCopy.sort((a,b) => {return b - a});
for(index = 0; index < nums.length; index++)
{
switch(arrayCopy.indexOf(nums[index]))
{
case 0:
{
nums[index] = 'Gold Medal';
break;
}
case 1:
{
nums[index] = 'Silver Medal';
break;
}
case 2:
{
nums[index] = 'Bronze Medal';
break;
}
default:
{
nums[index] = (arrayCopy.indexOf(nums[index]) + 1).toString();
}
}
}
return nums;
};
console.log(findRelativeRanks([5, 4, 3, 2, 1]));
leetCode刷题记录65_506_Relative Ranks
最新推荐文章于 2019-04-08 15:56:41 发布