LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次
506. Relative Ranks
题目描述
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予“金牌”,“银牌”和“铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。
(注: 分数越高的选手,排名越靠前。)
每日一算法2019/6/11Day 39LeetCode506. Relative Ranks
示例 1:
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予“金牌”,“银牌”和“铜牌”("Gold Medal", "Silver Medal" and "Bronze Medal")。
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
提示:
- N 是一个正整数并且不会超过 10000。
- 所有运动员的成绩都不相同。
Java 实现
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
class Solution {
public String[] findRelativeRanks(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
int n = nums.length;
String[] res = new String[n];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(nums[i], i);
}
Arrays.sort(nums);
for (int i = n - 1; i >= 0; i--) {
String str = String.valueOf(n - i);
if (n - i == 1) {
str = "Gold Medal";
} else if (n - i == 2) {
str = "Silver Medal";
} else if (n - i == 3) {
str = "Bronze Medal";
}
res[map.get(nums[i])] = str;
}
return res;
}
}
参考资料
LeetCode 506. 相对名次(Relative Ranks) 39的更多相关文章
- [Swift]LeetCode506. 相对名次 | Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- Java实现 LeetCode 506 相对名次
506. 相对名次 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 "金牌","银牌" 和" 铜牌&q ...
- 【leetcode】506. Relative Ranks
problem 506. Relative Ranks solution1:使用优先队列: 掌握priority_queue 和 pair的使用: class Solution { public: v ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- [LeetCode] Relative Ranks 相对排名
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 ...
- [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 ...
随机推荐
- junit4的进一步探讨
上次只是大概记录了下junit4几个常见标签的用法. 在这篇文章中,我们来进一步分析junit4的用法. 1.断言 junit4中一个很常见的用法就是断言.说到断言,大家再熟悉不过了.不过也许有的朋友 ...
- java 线程安全(初级)
创建和启动Java线程 Java线程是个对象,和其他任何的Java对象一样.线程是类的实例java.lang.Thread,或该类的子类的实例.除了对象之外,java线程还可以执行代码. 创建和启动线 ...
- .NET添加新项目-配置不同环境参数
添加新项目-配置不同环境参数 添加新项目后,需要对配置管理器进行设置.默认新加的项目只有debug和release 现加其他环境(dev.uat...)的配置[通过项目文件.csproj来加,拷贝其他 ...
- NOIP 2018 普及组 解题报告
目录 标题统计 题目链接 思路 代码 龙虎斗 题目链接: 思路 代码 摆渡车 题目链接: 思路 对称二叉树 题目链接 思路: 先来解释一下为毛现在才来发解题报告: 其实博主是参加过NOIP 2018普 ...
- 微信小程序七夕节礼物
VSCode Node.js HbuilderX 安装前端开发环境 [外链图片转存失败(img-aXUJRfXc-1565136341881)(https://upload-images.jiansh ...
- Android入门教程(三)
对Android五大布局的描述,分别是 FrameLayout (框架布局),LinearLayout (线性布局),AbsoluteLayout (绝对布局),RelativeLayout (相对布 ...
- 图解CRM(客户关系管理)全流程
https://blog.csdn.net/lylmwt/article/details/84921432
- ORACLE多条件的统计查询(case when)
前几天要做一个统计查询的功能,因为涉及多张表,多种条件的统计分析.一开始便想到了UNION和IF语句,然后写了1000多行代码,就为了查30条数据觉得不应该. 然后就开始百度,多种条件下的统计.然后有 ...
- samba-centos7
目的: 1,匿名访问共享目录/home/home and /home/share 2,/home/samba/home 共享名为home,有读写权限,但是进入该文件夹需要验证用户 3,/home/sa ...
- [C++] 浅拷贝和深拷贝
浅拷贝只是简单的值拷贝: 深拷贝需要重新分配空间. 系统默认的拷贝构造函数属于浅拷贝. #include <iostream> using namespace std; class A { ...