Java实现 LeetCode 506 相对名次
506. 相对名次
给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”(“Gold Medal”, “Silver Medal”, “Bronze Medal”)。
(注:分数越高的选手,排名越靠前。)
示例 1:
输入: [5, 4, 3, 2, 1]
输出: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” (“Gold Medal”, “Silver Medal” and “Bronze Medal”).
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
提示:
N 是一个正整数并且不会超过 10000。
所有运动员的成绩都不相同。
class Solution {
public String[] findRelativeRanks(int[] nums) {
int len = nums.length;
String[] result = new String[len];
if (len<1)return result;
//1、可以把数组转换成键值对(下标:成绩)按成绩排序,最后取出对应名次
//2、分数总量不大,不超过10000,可以用计数排序
int min = 0;
for (int num : nums) {
min = num>min?num:min;
}
int[] map = new int[min+1];
for (int num : nums) {
map[num]++;
}
//把map中每个分数对应的人数,转换成对应名次
int rank = 1;int temp = 0;
for (int i = min; i >= 0; i--) {
if (map[i]!=0){
temp = map[i];
map[i] = rank;
rank += temp;
}
}
//把名次映射成金银铜奖"Gold Medal", "Silver Medal", "Bronze Medal"
String s = "";int value = 0;
for (int i = 0; i < len; i++) {
value= map[nums[i]];
switch (value){
case 1: s = "Gold Medal";break;
case 2: s = "Silver Medal";break;
case 3: s = "Bronze Medal";break;
default: s = String.valueOf(value);
}
result[i] = s;
}
return result;
}
}
Java实现 LeetCode 506 相对名次的更多相关文章
- LeetCode 506. 相对名次(Relative Ranks) 39
506. 相对名次 506. Relative Ranks 题目描述 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予"金牌",&qu ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- 第一行Kotlin系列(一)kotlin按钮点击事件
按钮findViewBuId <Button android:id="@+id/mButton4" android:layout_width="wrap_conte ...
- CodeForces-650B Image Preview 二分+模拟
CodeForces-650B Image Preview 题意 手机里有n张图片,打开相机出现的是第一张,第一张右滑得到第n张,同理第n张左滑得到第1张,翻页耗费a秒,看照片耗费1s,但是照片有横屏 ...
- ASA failover配置(A/S)
环境描述 1. 两条公网出口,分别为移动,联通 2. 两台ASA做主备配置,实现出口故障转移 3. 内网两台核心做堆叠配置(由于模拟器无法实现堆叠,此处使用HSRP) 需求描述 1. 当一条公网链路故 ...
- html5 canvas画云
使用函数画出天空的云层图像: y 主要使用到的是数学的圆与弧度之间转换关系: 代码如下 //div对象 var parentContainer = document.getElementById(&q ...
- 使用python对oracle进行简单性能测试
一.概述 dba在工作中避不开的两个问题,sql使用绑定变量到底会有多少的性能提升?数据库的审计功能如果打开对数据库的性能会产生多大的影响?最近恰好都碰到了,索性做个实验. sql使用绑定变量对性能的 ...
- 挺好用的socks5库go-socks5
1.挺好用的socks5库 github.com/armon/go-socks5 2.示例代码 // Create a SOCKS5 server conf := &socks5.Config ...
- PART(Persistent Adaptive Radix Tree)的Java实现源码剖析
论文地址 Adaptive Radix Tree: https://db.in.tum.de/~leis/papers/ART.pdf Persistent Adaptive Radix Tree: ...
- 轻松解决python异常处理,你值得拥有
目录 python中常见的异常信息+处理方法 常见异常类型 异常处理 python中常见的异常信息+处理方法 常见异常类型 异常类名 功能描述 Exception 所有异常的基类 ValueError ...
- Kubernetes学习笔记(二):部署托管的Pod -- 存活探针、ReplicationController、ReplicaSet、DaemonSet、Job、CronJob
存活探针 Kubernetes可以通过存活探针(liveness probe)检查容器是否存活.如果探测失败,Kubernetes将定期执行探针并重新启动容器. 官方文档请见:https://kube ...
- .net System.Web.Mail发送邮件 (设置发件人 只显示用户名)
http://blog.163.com/hao_2468/blog/static/130881568201141251642215/ .net System.Web.Mail发送邮件 2011-05- ...