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".
Note:
- N is a positive integer and won't exceed 10,000.
- 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问题及解法相关推荐
- LeetCode | 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode之Relative Ranks
1.题目 Given scores of N athletes, find their relative ranks and the people with the top three highest ...
- [LeetCode]506. Relative Ranks
[LeetCode]506. Relative Ranks 题目描述 思路 用map保存 计算res的时候直接逆序遍历map即可 代码 #include <iostream> #inclu ...
- LeetCode Relative Ranks
今天做了大概四五道题,但觉着这道题是最值得分享的吧,Relative Ranks意思是相对排名问题,下面是题目具体内容,大家可以通过翻译试着做一下: Given scores of N athlete ...
- Leetcode算法题-解法转载
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...
- LeetCode 力扣算法题解汇总,All in One
作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
- LeetCode 简单算法题
使用Nodejs 抓取的LeetCode 简单算法题 一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...
- c语言while语句写法,C语言while语句
在C语言中,共有三大常用的程序结构: 顺序结构:代码从前往后执行,没有任何"拐弯抹角": 选择结构:也叫分支结构,重点要掌握 if else.switch 以及条件运算符: 循环结 ...
- java中Solution怎么用_【leetcode】solution in java——Easy4
16:Invert Binary Tree 此题:以根为对称轴,反转二叉树. 思路:看到二叉树,我们第一时间要想到处理二叉树的常用方法--BFS.DFS,更常用的是DFS.此题我们先用BFS来思考:B ...
最新文章
- 一个打印机的希奇古怪的问题二
- Swing如何正确的处理界面中的线程(EDT)
- 正则截取指定字符串 php,php截取指定字符串除了正则还有什么方法
- java.lang.InstantiationException 不能实例化某个对象
- 什么是 SAP Fiori Tools
- 编译型语言和解释型语言区别,编译器,解释器的区别
- oracle 08177,Java OracleDatabaseException: ORA-08177: 无法连续访问此事务处理问题解决
- Spark源码剖析(一):如何将spark源码导入到IDEA中
- 24.redis持久化之AOF
- 16.看板方法---三类改进机会
- Python 用装饰器便捷实现多线程
- 软件工程(吕云翔第二版)部分知识点
- 移动办公模式下的业务 微易聊微信管理系统作用显著!
- 多少秒算长镜头_电影中什么是长镜头画面(9个经典长镜头)
- html之菜单栏设置
- R语言绘图、高清保存、以及颜色系统
- biu biu biu
- 特斯拉蛇形充电机器人_特斯拉也造出蛇形机器人,专为充电使用!
- VirtualBox 虚拟机上网配置-实现多台虚拟机局域网独立ip,可连接外网
- 臻米脱糖电饭煲,古法先煮后蒸,实现低糖生活