【发布时间】:2018-10-07 02:18:55
【问题描述】:
这是接收两个数组作为参数的方法, 包含重复值的分数数组(按降序排列。),我删除了重复值并 将其存储在一个没有重复的新数组中, 第二个数组包含特殊的玩家分数。
我需要评估她在分数数组中的排名 她的数组中的每个分数。 我可以用 for 循环来做,但这需要很长时间,我尝试使用 Array .IndexOf 方法,但对于不存在的值,我得到了 -1。
代码:
static int[] climbingLeaderboard(int[] scores, int[] alice)
{
var aliceRecord = new List<int>();
int[] oneArray;
oneArray = scores.Distinct().ToArray();
foreach (var aliceScore in alice)
{
if (aliceScore < oneArray[oneArray.Length - 1])
{
aliceRecord.Add(oneArray.Length + 1);
}
else
{
var rank = Array.IndexOf(oneArray, aliceScore);
if (rank < 0)
{
//Here I need the help
//I comented the un efficient code
//for (int i = 0; i < oneArray.Length; i++)
//{
// if (aliceScore >= oneArray[i])
// {
// aliceRecord.Add(i + 1);
// break;
// }
//
//
//}
}
else
{
aliceRecord.Add(rank + 1);
}
}
}
return aliceRecord.ToArray();
}
【问题讨论】:
-
而
alice数组没有排序? -
不,不是,出于问题(问题)的目的,我无法对其进行排序,因为我需要根据她的分数记录她的历史记录
标签: c# arrays performance indexof coding-efficiency