zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    asterisk 使用 g729 g723
    读书笔记《一线架构师》
    能和LoadRunner匹敌的VS2010/2012Web负载测试
    总结 设计模式,企业应用架构模式,架构模式
    聊聊Memcached的应用
    WPF小试牛刀
    读书笔记《Hadoop开源云计算平台》
    高性能与可扩展
    闲聊下架构、框架,以及架构师...
    自己写框架 实践 (Event Framework)
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/6665157.html
Copyright © 2011-2022 走看看