Leetcode 506(Java)
来源:互联网 发布:linux locale命令 编辑:程序博客网 时间:2024/05/18 17:04
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:
N is a positive integer and won’t exceed 10,000.
All the scores of athletes are guaranteed to be unique.
AC码:
public class Solution { public String[] findRelativeRanks(int[] nums) { Integer[] num = new Integer[nums.length]; for(int i=0;i<nums.length;i++){ num[i] = nums[i]; } Comparator<Integer> compr = new Comparator<Integer>(){ public int compare(Integer m,Integer n){ return n.compareTo(m); } }; Arrays.sort(num, compr); HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); int n=1; for(int x:num){ map.put(x, n); n++; } String[] result = new String[nums.length]; int index = 0; for(int y:nums){ switch (map.get(y)){ case 1: result[index] = "Gold Medal"; break; case 2: result[index] = "Silver Medal"; break; case 3: result[index] = "Bronze Medal"; break; default: result[index] = Integer.toString(map.get(y)); break; } index++; } return result; }}
0 0
- Leetcode 506(Java)
- leetcode整理(java)
- Leetcode 543(Java)
- Leetcode 389(Java)
- Leetcode 387(Java)
- Leetcode 207(Java)
- leetcode 49(java)
- Leetcode 463(Java)
- Leetcode 22(Java)
- Leetcode 151(Java)
- Leetcode 628(Java)
- Leetcode 645(Java)
- LeetCode-Java (一)
- Leetcode-Java (二)
- Leetcode-Java (三)
- Leetcode-Java (四)
- Leetcode(java)
- leetcode java
- 机器学习和深度学习的最佳框架
- Gulp新手入门教程
- nodejs+socket.io实现校内局域网聊天室
- 莫比乌斯反演
- Set、Map、list的排序,应用及案例
- Leetcode 506(Java)
- JAVA-13-IO流之字符流;设计模式讲解
- 深度学习这么火,但要小心不要被它忽悠了
- Android app接入百度地图:禁用手势、模式切换、根据经纬度获取位置信息
- CSS3中的动画功能
- Spark2.1.0+Hadoop2.7.3关联规则平台(三)
- webpack + vue 笔记
- Result Maps collection does not contain value for java.lang.Long
- Hibernate第十二篇【二级缓存介绍、缓存策略、查询缓存、集合缓存】