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.
Note:
- 1.N is a positive integer and won't exceed 10,000.
- 2.All the scores of athletes are guaranteed to be unique.
这道题目的意思是返回数组中每一个元素的排名,不能直接排序这样会打乱每一个元素的原始位置,因为题目上面说数组中的每一个值都是唯一的,所以可以用python的字典方面的找出排序。
这里我使用的是leetcode其他用户给出的java写法,主要是因为里面的lambda
新特性。
public String[] findRelativeRanks(int[] nums) {
int pair[][] = new int[nums.length][2];
for(int i = 0; i < nums.length; i++)
{
pair[i][0] = nums[i];
pair[i][1] = i;
}
Arrays.sort(pair,(a,b) -> (b[0] - a[0]));
String result[] = new String[nums.length];
for(int i = 0; i < nums.length; i++)
{
if (i == 0)
result[pair[i][1]] = "Gold Medal";
else if (i == 1)
result[pair[i][1]] = "Silver Medal";
else if (i == 2)
result[pair[i][1]] = "Bronze Medal";
else
result[pair[i][1]] = (i + 1) + "";
}
return result;
}
上面连接已经给出了详解,我在啰嗦一下pair数值发生变化
10, 0
3, 1
8, 2
9, 3
4, 4
排序后
10, 0
9, 3
8, 2
4, 4
3, 1
详细的lambda介绍可见这里
506. Relative Ranks的更多相关文章
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- 506 Relative Ranks 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- [LeetCode] Relative Ranks 相对排名
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [Swift]LeetCode506. 相对名次 | Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode] 506. Relative Ranks_Easy tag: Sort
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- LeetCode Relative Ranks
原题链接在这里:https://leetcode.com/problems/relative-ranks/#/description 题目: Given scores of N athletes, f ...
随机推荐
- selenium 使用xpath定位不到
<button id="" class="btn btn-some" type="submit"> <i class=&q ...
- [epub] epub.js的ePubReader函数报URI malformed错误的解决办法
报错信息:URI malformed 今天遇到了一个奇怪的问题折腾三个小时,最后发现是作者在底层使用了decodeURIComponent进行URL解码,而我在应用层使用了escape/unescap ...
- Nomad入门
Nomad 简介 Nomad是一个管理机器集群并在集群上运行应用程序的工具. Nomad的特点: 支持docker,Nomad的job可以使用docker驱动将应用部署到集群中. Nomad安装在li ...
- TCP/IP协议栈 ARP和RARP协议
上几章中我们提到以太网协议中,在以太网首部中一个帧类型的字段,它可以表示为IP ARP RARP协议. 这里说一下ARP 和RARP协议. 首先看ARP协议: 要想网络中的数据包准确到达某个主机,最后 ...
- google软件测试之道读后感(一)
这几天在抽空读一本新书,久负盛名的<google软件测试之道>.之前在网络上一点一点地看过它的英文版,很受触动,还做了很长的读书笔记,现在看到了中文版,才恍觉之前的好些理解存在不恰当的地方 ...
- git学习资料包
1.廖雪峰老师的git教程:https://www.liaoxuefeng.com -----点击“GIT教程”开始学习 2.菜鸟教程git学习:http://www.runoob.com/gi ...
- 初识java这个小姑娘(一)
忽然想起这样一个场景:那时我还是小学三年级的一个小学生,上课的铃声响起,文艺委员起头,大家开始胡乱的开始唱歌,"让我们荡起双桨,小船儿推开波浪",歌声在一片稚气中慢慢停止.我们的语 ...
- ElasticSearch 学习记录之ES短语匹配基本用法
短语匹配 短语匹配故名思意就是对分词后的短语就是匹配,而不是仅仅对单独的单词进行匹配 下面就是根据下面的脚本例子来看整个短语匹配的有哪些作用和优点 GET /my_index/my_type/_sea ...
- 》》webpack打包成的文件
/******/(function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installed ...
- Android之通过网络播放一首简单的音乐
首先,附上程序执行后的效果.例如以下图所看到的: 一.部署一个web项目到tomcatserver上: 1.这个小程序是结合网络来播放一首音乐的,首先,把我们搞好的一个web项目放置在tomcat安装 ...