zoukankan      html  css  js  c++  java
  • 506 Relative Ranks 相对名次

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。
    (注:分数越高的选手,排名越靠前。)
    示例 1:
    输入: [5, 4, 3, 2, 1]
    输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
    解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").
    余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
    提示:
        N 是一个正整数并且不会超过 10000。
        所有运动员的成绩都不相同。
    详见:https://leetcode.com/problems/relative-ranks/description/

    C++:

    class Solution {
    public:
        vector<string> findRelativeRanks(vector<int>& nums) 
        {
            int n = nums.size(), cnt = 1;
            vector<string> res(n, "");
            map<int, int> m;
            for (int i = 0; i < n; ++i)
            {
                m[nums[i]] = i;
            }
            for (auto it = m.rbegin(); it != m.rend(); ++it) 
            {
                if (cnt == 1)
                {
                    res[it->second] = "Gold Medal";
                }
                else if (cnt == 2)
                {
                    res[it->second] = "Silver Medal";
                }
                else if (cnt == 3)
                {
                    res[it->second] = "Bronze Medal";
                }
                else
                {
                    res[it->second] = to_string(cnt);
                }
                ++cnt;
            }
            return res;
        }
    };
    

     参考:http://www.cnblogs.com/grandyang/p/6476983.html

  • 相关阅读:
    iOS工作笔记(十二)
    隐藏底部导航栏tabbar
    SDWebImage的使用及图片不改变问题的解决
    NSString转化为NSDictionary
    makeObjectsPerformSelector用法
    iOS中点与像素
    iOS动画的简单介绍
    Pods安装时卡在Updating local specs repositories界面时的处理
    使用uicollectionView时需要注意的问题
    Sublime Text 3 自定义配置快捷键
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8907349.html
Copyright © 2011-2022 走看看