目录
1.题目:
2.博主的想法:
3.更好的思想:
题目:
/*给你一个长度为 n 的整数数组 score ,其中 score[i] 是第 i 位运动员在比赛中的得分。所有得分都 互不相同 。
运动员将根据得分 决定名次 ,其中名次第 1 的运动员得分最高,名次第 2 的运动员得分第 2 高,依此类推。运动员的名次决定了他们的获奖情况:
名次第 1 的运动员获金牌 "Gold Medal" 。
名次第 2 的运动员获银牌 "Silver Medal" 。
名次第 3 的运动员获铜牌 "Bronze Medal" 。
从名次第 4 到第 n 的运动员,只能获得他们的名次编号(即,名次第 x 的运动员获得编号 "x")。
使用长度为 n 的数组 answer 返回获奖,其中 answer[i] 是第 i 位运动员的获奖情况。
示例 1:
输入:score = [5,4,3,2,1]
输出:["Gold Medal","Silver Medal","Bronze Medal","4","5"]
解释:名次为 [1st, 2nd, 3rd, 4th, 5th] 。
示例 2:
输入:score = [10,3,8,9,4]
输出:["Gold Medal","5","Bronze Medal","Silver Medal","4"]
解释:名次为 [1st, 5th, 3rd, 2nd, 4th] 。*/
博主的想法:
1.看完题目后,根据要求我们需要把数组中成员的成绩,找到其对应的奖励名次,然后把名次存入字符串数组中,最后返回该字符串数组
核心思想:
1.定义一个方法,这个方法用于找到数组中最大成绩的那个值的索引,然后把该数值改成-1(因为成绩最低只能是0,所以,改成-1下次寻找该数组的其他最大元素就很方便)
private int getMaxIndex(int[] score) {
int max = -1;
int j = 0;
for (int i = 0; i < score.length; i++) {
if (max < score[i]) {
max = score[i];
j = i;
}
}
score[j] = -1;
return j;
}
2.通过循环连续调用上述方法,一点一点的找出当前数组的最大值的索引,并在循环中给一个计数器变量,每循环一次就会找出当前数组中最大的值,计数器就会+1,并当计数器在1,2,3的时候通过得到的索引,把对应的奖励名次存入字符串数组中。
public String[] findRelativeRanks(int[] score) {
int count = 0;
int i = 4;
String[] answer = new String[score.length];//存入奖励的数组
for (int i1 = score.length; i1 > 0; i1--) {
//先遍历数组去寻找数组中最大的那个数字的索引
count++;
int index = getMaxIndex(score);
if (count == 1) {
answer[index] = "Gold Medal";
}
if (count == 2) {
answer[index] = "Silver Medal";
}
if (count == 3) {
answer[index] = "Bronze Medal";
}
if (count != 1 && count != 2 && count != 3) {
answer[index] = Integer.toString(i);
i++;
}
}
return answer;//返回奖励数组
}
博主的代码:
import java.util.Arrays;
class Solution {
public String[] findRelativeRanks(int[] score) {
int count = 0;
int i = 4;
String[] answer = new String[score.length];//存入奖励的数组
for (int i1 = score.length; i1 > 0; i1--) {
//先遍历数组去寻找数组中最大的那个数字的索引
count++;
int index = getMaxIndex(score);
if (count == 1) {
answer[index] = "Gold Medal";
}
if (count == 2) {
answer[index] = "Silver Medal";
}
if (count == 3) {
answer[index] = "Bronze Medal";
}
if (count != 1 && count != 2 && count != 3) {
answer[index] = Integer.toString(i);
i++;
}
}
return answer;//返回奖励数组
}
private int getMaxIndex(int[] score) {
int max = -1;
int j = 0;
for (int i = 0; i < score.length; i++) {
if (max < score[i]) {
max = score[i];
j = i;
}
}
score[j] = -1;
return j;
}
}
public class Test {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(Arrays.toString(solution.findRelativeRanks(new int[]{5, 4, 3, 2, 1})));
System.out.println(Arrays.toString(solution.findRelativeRanks(new int[]{10, 3, 8, 9, 4})));
System.out.println(Arrays.toString(solution.findRelativeRanks(new int[]{123123, 11921, 1, 0, 123})));
}
}
更好的思想:
1.该思想是定义一个数组长度为行,两列的二维数组,第一行存入的是数组中的成绩,第二行是成绩在数组中对应的索引,
2.通过sort的重载方法对二维数组进行排序,排序的规则就是按照第一列的成绩从高到底把行进行排序,这个时候其第二行的索引也进行了对应的排序,
3.此时就可以通过遍历二维数组的方式,前三个肯定是排名第一第二第三此时就可以把对应的名次根据其对应的索引存入字符串数组中,

更好的核心代码:
public String[] findRelativeRanks(int[] score) {
int n = score.length;//数组的长度
String[] desc = {"Gold Medal", "Silver Medal", "Bronze Medal"};//用该数组存入奖励
int[][] arr = new int[n][2];//用二维数组,一共n行2列
//第一列用来存入成绩,第二列是每个成绩对应的索引
for (int i = 0; i < n; ++i) {
arr[i][0] = score[i];//把成绩存入二维数组的第一列
arr[i][1] = i;//先把现在的成绩给它一个对应的索引
}
//用sort方法排序二维数组,b[0]a[0]是代表的行 ,
//在排序的时候,先比该行的第一个元素,如果相等在向后比第二个元素
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
String[] ans = new String[n];//该数组用来根据成绩存入奖励排名
for (int i = 0; i < n; ++i) {
if (i >= 3) {
ans[arr[i][1]] = Integer.toString(i + 1);
} else {
ans[arr[i][1]] = desc[i];
}
}
return ans;
}

250

被折叠的 条评论
为什么被折叠?



