问题描述:

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".

Note:

  1. N is a positive integer and won't exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

示例:

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.

问题分析:

利用hashtable求解即可,过程详见代码:

class Solution {
public:vector<string> findRelativeRanks(vector<int>& nums) {vector<int> nums2 = nums;sort(nums.begin(),nums.end());map<int,int> m;vector<string> res;for(int i = nums.size() - 1,j = 1;i >= 0 ; i--){m.insert(pair<int,int>(nums[i],j++));}for(int i = 0; i < nums2.size(); i++){if(m[nums2[i]] == 1) res.push_back("Gold Medal");else if(m[nums2[i]] == 2) res.push_back("Silver Medal");else if(m[nums2[i]] == 3) res.push_back("Bronze Medal");else res.push_back(to_string(m[nums2[i]])); // c++ 11支持 }return res;}
};

Relative Ranks问题及解法相关推荐

  1. LeetCode | 506. Relative Ranks

    Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...

  2. LeetCode之Relative Ranks

    1.题目 Given scores of N athletes, find their relative ranks and the people with the top three highest ...

  3. [LeetCode]506. Relative Ranks

    [LeetCode]506. Relative Ranks 题目描述 思路 用map保存 计算res的时候直接逆序遍历map即可 代码 #include <iostream> #inclu ...

  4. LeetCode Relative Ranks

    今天做了大概四五道题,但觉着这道题是最值得分享的吧,Relative Ranks意思是相对排名问题,下面是题目具体内容,大家可以通过翻译试着做一下: Given scores of N athlete ...

  5. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  6. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  8. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

  9. c语言while语句写法,C语言while语句

    在C语言中,共有三大常用的程序结构: 顺序结构:代码从前往后执行,没有任何"拐弯抹角": 选择结构:也叫分支结构,重点要掌握 if else.switch 以及条件运算符: 循环结 ...

  10. java中Solution怎么用_【leetcode】solution in java——Easy4

    16:Invert Binary Tree 此题:以根为对称轴,反转二叉树. 思路:看到二叉树,我们第一时间要想到处理二叉树的常用方法--BFS.DFS,更常用的是DFS.此题我们先用BFS来思考:B ...

最新文章

  1. 一个打印机的希奇古怪的问题二
  2. Swing如何正确的处理界面中的线程(EDT)
  3. 正则截取指定字符串 php,php截取指定字符串除了正则还有什么方法
  4. java.lang.InstantiationException 不能实例化某个对象
  5. 什么是 SAP Fiori Tools
  6. 编译型语言和解释型语言区别,编译器,解释器的区别
  7. oracle 08177,Java OracleDatabaseException: ORA-08177: 无法连续访问此事务处理问题解决
  8. Spark源码剖析(一):如何将spark源码导入到IDEA中
  9. 24.redis持久化之AOF
  10. 16.看板方法---三类改进机会
  11. Python 用装饰器便捷实现多线程
  12. 软件工程(吕云翔第二版)部分知识点
  13. 移动办公模式下的业务 微易聊微信管理系统作用显著!
  14. 多少秒算长镜头_电影中什么是长镜头画面(9个经典长镜头)
  15. html之菜单栏设置
  16. R语言绘图、高清保存、以及颜色系统
  17. biu biu biu
  18. 特斯拉蛇形充电机器人_特斯拉也造出蛇形机器人,专为充电使用!
  19. VirtualBox 虚拟机上网配置-实现多台虚拟机局域网独立ip,可连接外网
  20. 臻米脱糖电饭煲,古法先煮后蒸,实现低糖生活

热门文章

  1. 基本概念学习(7002)---网络流量控制
  2. OSChina 周一乱弹 ——听过煲仔饭吗?老婆饼呢?
  3. SM2 SM3 SM4 国密版本,基于bouncycastle 实现
  4. python空气质量提醒代码_【python】全国空气质量分析系统
  5. 舵机常见问题原理分析及解决办法
  6. 二、JavaScript 基础(上) - 章节课后练习题及答案
  7. vue图片时间轴滑动_在vue2.0 中制作时间轴拖动组件
  8. 蓝桥杯 方格计数 Java
  9. Java学习手册:大疆创新2020届校园招聘——软件类岗位B卷题二
  10. 微信小程序获取微信卡包发票原件及格式化发票信息