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.
    
    class Solution {
    public:
        static bool cmp(int a,int b){
            return a>b;
        }
        vector<string> findRelativeRanks(vector<int>& nums) {
              vector<string> res;
              map<int, int> rank;
              vector<int> temp;
              temp=nums;
              sort(nums.begin(),nums.end(),cmp);
              for(int i=0; i<nums.size(); i++)
                  rank[nums[i]]=i+1;
              for(int i=0; i<temp.size(); i++)
                  if(rank[temp[i]]==1)
                     res.push_back("Gold Medal");
                  else if(rank[temp[i]]==2)
                     res.push_back("Silver Medal");
                  else if(rank[temp[i]]==3)
                     res.push_back("Bronze Medal");
                  else 
                     res.push_back(to_string(rank[temp[i]]));
            return res;
        }
    };
    
  • 相关阅读:
    下雪诗
    华视身份证阅读器100UC HTTP模式二次开发
    C# Action 和 Func 区别
    网站部署——文件系统
    前端-JavaScript DOM和BOM
    IO多路复用
    python-协程
    python-线程
    python-进程
    计算机与操作系统简介
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/10073825.html
Copyright © 2011-2022 走看看