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 ...
随机推荐
- 【Spark】Spark-shell案例——standAlone模式下读取HDFS上存放的文件
目录 可以先用local模式读取一下 步骤 一.先将做测试的数据上传到HDFS 二.开发scala代码 standAlone模式查看HDFS上的文件 步骤 一.退出local模式,重新进入Spark- ...
- Day_09【常用API】扩展案例8_计算字符'j'和字符串'java'在字符串中出现的次数
需求说明 定义如下字符串: String str = "javajfiewjavajfiowfjavagkljjava"; 请分别定义方法统计出: 1.字符串中:字符j的数量 2. ...
- input唤起键盘影响移动端底部fixed定位
主要代码如下: public docmHeight = document.documentElement.clientHeight || document.body.clientHeight; // ...
- 计算机组成及系统结构-第十章 输入输出(I/O)系统
输入输出(I/O)系统 一.概述 1.输入输出设备的编址 2.设备控制器(I/O接口)的基本功能 3.I/O设备数据传送控制方式 二.程序中断输入输出方式 1.中断的定义 2.中断的作用 3.中断的产 ...
- python --字符串学习
一 转义字符 借用一个特殊的方法表示一系列不方便写出的内容,比如回车键,换行符,退格键 借助反斜杠字符,一旦出现反斜杠,则反斜杠后面一个或者几个字符表示已经不是原来的意思了 在字符串中,一旦出现了斜杠 ...
- Windows基础学习
0x01 常用的端口 HTTP协议代理服务器常用端口号:80/8080/3128/8081/9098SOCKS代理协议服务器常用端口号:1080FTP(文件传输)协议代理服务器常用端口号:21Teln ...
- SpringMVC 拦截返回值,并自定义
有关取代mvc:annotation-driven使用自定义配置请看: http://blog.csdn.net/cml_blog/article/details/45222431 1.在项目开发中, ...
- Codeforces 909E(Coprocessor,双队列维护)
题意:给出n个待处理的事件(0 ~n-1),再给出了n个标(0表示只能在主处理器中处理这个事件,1表示只能在副处理器中处理这个事件),处理器每次能处理多个任务.每个事件有关联,如果一个任务要在副处理器 ...
- mysql 获取当前指定分钟的时间
SELECT NOW(); MINUTE); 结果:
- SSD1306(OLED驱动芯片)指令详解
基础命令: 1.页地址模式下设置列起始地址低位(Set Lower Column Start Address For Page Addressing Mode)(00h~0Fh) 此指令用于在页地址模 ...