17,742
社区成员
发帖
与我相关
我的任务
分享
int[] a = { 1, 2, 3, 5, 52, 110 };
List<int> result = new List<int>() { a[0] };
IEnumerable<int> r = a.Skip(1);
while (true)
{
r = r.SkipWhile(x => x - result[result.Count - 1] < 50);
if (!r.Any()) break;
result.Add(r.First());
}
foreach (var item in result)
Console.WriteLine(item);
static int[] Find(int[] sortedArray)
{
if (sortedArray == null || sortedArray.Length == 0)
{
return new int[] { };
}
var cache = new List<int>(sortedArray.Length);
int cursor = sortedArray[0];
cache.Add(cursor);
for (int i = 1; i < sortedArray.Length; i++)
{
if (sortedArray[i] - cursor >= 50)
{
cursor = sortedArray[i];
cache.Add(cursor);
}
}
return cache.ToArray();
}
int[] a = { 1, 2, 3, 5, 52, 110 };
var results = Find(a);
版大,你的方式不太可取,如果出现
int[] a = {1,2,3,5,52,, 101, 110};
你的方法会取到101,而不是110,而101-52 < 50