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;
        }
    };
    
  • 相关阅读:
    动网16位gb2312md5加密
    开发windows7侧边栏小工具
    MVC文档地址
    关闭FCNs(文件修改监控)
    内存管理
    android笔记一(Button)
    android笔记五ImageButton
    android笔记三FrameLayout
    linux内核各组件的功能介绍
    C++面试题
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/10073825.html
Copyright © 2011-2022 走看看