题目:

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.

Note:

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

题目链接

题意:

有n个作家的分数,分数唯一不重复,要求给作家按分数排名,第一名是“Gold Medal”, 第二名是“Silver Medal”, 第三名是“Bronze Medal”,其他的名次将int转化为string。

解决方法是用一个数组来记录每一个分数的名次位置,在排序的时候把这个数组的变化也加上,最后可以直接通过分数查询名词。

代码如下:

class Solution {
public:vector<string> findRelativeRanks(vector<int>& nums) {int a[1000010];vector<int>temp = nums;memset(a, -1, sizeof a);for (int i = 0; i < nums.size(); i ++) {a[nums[i]] = i + 1;}for (int i = 0; i < temp.size(); i ++) {for (int j = 0; j < i; j ++) {if (temp[i] > temp[j]) {swap(temp[i], temp[j]);swap(a[temp[i]], a[temp[j]]);}}}vector<string> ans;for (int i = 0; i < nums.size(); i ++) {int pos = a[nums[i]];if (pos == 1) {ans.push_back("Gold Medal");}   else if (pos == 2) {ans.push_back("Silver Medal");}else if (pos == 3) {ans.push_back("Bronze Medal");}else {ans.push_back(to_string(pos));}}return ans;}
};

其中用于记录名次的数组用map代替可以。

LeetCode 506 Relative Ranks相关推荐

  1. [LeetCode]506. Relative Ranks

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

  2. LeetCode | 506. Relative Ranks

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

  3. LeetCode之Relative Ranks

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

  4. LeetCode Relative Ranks

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

  5. LeetCode 506. 相对名次(map)

    1. 题目 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌. 前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌" ...

  6. leetcode 506. 相对名次(Java版)

    题目 https://leetcode-cn.com/problems/relative-ranks/ 题解 思路主要是,将运动员一开始的位置记录下来,方便颁奖的时候去指定位置颁给指定的人. 先用ma ...

  7. leetcode 506 相对名次

    https://leetcode-cn.com/problems/relative-ranks/ 题目 给你一个长度为nnn的整数数组scorescorescore,其中score[i]score[i ...

  8. LeetCode 简单算法题

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

  9. Leetcode算法题-解法转载

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

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

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

最新文章

  1. Python基础-安装
  2. JavaScript之引用类型
  3. 谈谈你对面向过程与面向对象的理解
  4. css3抽奖转盘,从零制作CSS3抽奖大转盘
  5. docker centos 环境 安装 python
  6. python selenium chrome获取每个请求内容_python+selenium调用chrome打开网址获取内容
  7. 基于数据库实现分布式锁
  8. Spring框架学习笔记02:初探Spring——利用组件注解符精简Spring配置文件
  9. 因弹幕系统技术升级 B站即日起至6月6日关闭弹幕功能
  10. Java for LintCode 验证二叉查找树
  11. 解决Error: ENOENT: no such file or directory, scandir ‘xxx\node-sass\vendor‘
  12. 解决小程序背景图片在真机上不能查看的问题
  13. Nginx篇--解读nginx配置
  14. Ubuntu14.4安装mysql
  15. 手机怎么用java9_java9_java9官方版 32位64位 最新版_天天下载手机版
  16. SQLyog安装成功步骤(附带码),2021版最新
  17. LNMP(Nginx服务,MySQL 服务,安装PHP服务 手动安装技术文档)
  18. SameSite 属性
  19. JavaScript JQuery 交互式Web前端开发
  20. Codeblock 美化字体和主题

热门文章

  1. elementUI按需引入时报错 :Cannot find module ‘babel-preset-es2015‘
  2. 学 C++ ,能不能简单点?
  3. java bouncycastle_BouncyCastle
  4. 好用的空气质量查询API
  5. 【详细】【转】CentOS 7部署ASP.NET Core应用程序
  6. excel表格打印每页都有表头_教你一招Excel打印技巧:每页有表头标题,阅读起来是真方便...
  7. 约翰霍普金斯大学计算机专业,约翰霍普金斯大学计算机专业排名第几?
  8. 百万邮做邮件营销的邮箱配置
  9. 外媒评出世界十大地质奇迹
  10. 2019HPU-ICPC-Training-1