• [leetcode-506-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:
    N is a positive integer and won't exceed 10,000.
    All the scores of athletes are guaranteed to be unique.

    思路:

    思路很朴素,首先就是排序,然后定义一个map,储存对应分数及其排名信息。最后再遍历一遍原来的分数数组,对应每一个分数

    给定相应的排名。注意,要提前备份原来的数组,因为排序之后数组顺序就变了。

    vector<string> findRelativeRanks(vector<int>& nums)
     {
         vector<int>backup = nums;
         vector<string>result;
         if(nums.size()==0)return result;
         sort(nums.begin(),nums.end(),greater<int>());
         map<int,string>table;
         char rank[10];
         for(int i = 0;i < nums.size();i++)
         {
             sprintf(rank,"%d",i+1);
             if(i == 0)table[nums[i]]="Gold Medal";
             else if(i == 1)table[nums[i]]="Silver Medal";
             else if(i == 2)table[nums[i]]="Bronze Medal";
             else  table[nums[i]] = rank;
         }
         result.push_back(table[backup[0]]);
         for(int i =1;i<nums.size();i++)
         {
                     result.push_back(table[backup[i]]);
         }
         return result;
    }
  • 相关阅读:
    用Web标准进行开发
    哪个是你爱情的颜色?
    由你的指纹,看你的性格。
    让你受用一辈子的181句话
    漂亮MM和普通MM的区别
    ASP构造大数据量的分页SQL语句
    随机码的生成
    爱从26个字母开始 (可爱的史努比)
    浅谈自动采集程序及入库
    值得收藏的JavaScript代码
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/6665157.html
Copyright © 2020-2023  润新知