C# 列表中的所有连续项<;int>;
我想创建一个像这样的扩展方法C# 列表中的所有连续项<;int>;,c#,linq,C#,Linq,我想创建一个像这样的扩展方法 public static bool AllConsecutives(this IEnumerable<int> intValues ) public static bool allcontinuences(此IEnumerable intValues) 如果列表中的所有项都是连续的(没有间隙),则此方法应返回true 一些测试用例 (new List<int>() {2, 3, 4, 5, 6}).AllConsecutives() =
public static bool AllConsecutives(this IEnumerable<int> intValues )
public static bool allcontinuences(此IEnumerable intValues)
如果列表中的所有项都是连续的(没有间隙),则此方法应返回true
一些测试用例
(new List<int>() {2, 3, 4, 5, 6}).AllConsecutives() == true
(new List<int>() {3, 7, 4, 5, 6}).AllConsecutives() == true //as it is not sensitive to list order
(new List<int>() {2, 3, 4, 7, 6}).AllConsecutives() == false //as the five is missing
(新列表(){2,3,4,5,6})。AllConcertives()==true
(新列表(){3,7,4,5,6})。AllConcertives()==true//因为它对列表顺序不敏感
(新列表(){2,3,4,7,6})。AllConcertives()==false//因为缺少五个
在执行Min
之前,您还可以验证集合至少有一个项。或者,您可以在获取min之前对项目进行排序(在这种情况下,它将是第一个项目,如果集合为空,则不会是任何项目)。同样,在这种情况下,您将保存一次迭代以查找最小值:
var sortedValues = intValues.OrderBy(x => x);
int expected = sortedValues.FirstOrDefault();
foreach (int actual in sortedValues)
{
if (actual != expected++)
return false;
}
return true;
大概是这样的:
if (intValues.Count() <= 1)
return true;
var ordered = intValues.OrderBy(i => i).ToList();
return (ordered.First() + ordered.Count() - 1) == ordered.Last();
if(intValues.Count()i).ToList();
return(ordered.First()+ordered.Count()-1)=ordered.Last();
如果列表中不包含重复项,并且最大值和最小值之间的差值等于列表中的项数减去一,则列表是连续的,因此:
public static bool AllConsecutives(this IEnumerable<int> intValues)
{
int minValue = Int32.MaxValue;
int maxValue = Int32.MinValue;
int count = 0;
HashSet<int> values = new HashSet<int>();
foreach (int intValue in intValues) {
if (values.Contains(intValue))
return false;
values.Add(intValue);
if (intValue > maxValue)
maxValue = intValue;
if (intValue < minValue)
minValue = intValue;
count++;
}
return (count == 0) || (maxValue-minValue+1 == count);
}
public static bool allcontinuences(此IEnumerable intValues)
{
int minValue=Int32.MaxValue;
int maxValue=Int32.MinValue;
整数计数=0;
HashSet值=新的HashSet();
foreach(intValues中的intValue){
if(values.Contains(intValue))
返回false;
value.Add(intValue);
如果(intValue>maxValue)
maxValue=intValue;
if(intValue
尝试并似乎与给定的示例配合使用
public static bool AllConsecutives(this IEnumerable<int> intValues )
{
var ord = intValues.OrderBy(i => i);
int curV = ord.Min();
foreach(int x in ord)
{
if(x != curV)
return false;
curV++;
}
return true;
}
public static bool allcontinuences(此IEnumerable intValues)
{
var ord=intValues.OrderBy(i=>i);
int curV=ord.Min();
foreach(ord中的整数x)
{
如果(x!=曲线)
返回false;
curV++;
}
返回true;
}
错误检查和使用Linq:
public static class myExtension
{
public static bool AllConsecutives(this IEnumerable<int> targetList)
{
bool result = false;
if ((targetList != null) && (targetList.Any ()))
{
var ordered = targetList.OrderBy (l => l);
int first = ordered.First ();
result = ordered.All (item => item == first++);
}
return result;
}
}
// tested with
void Main()
{
Console.WriteLine ( (new List<int>() {2, 3, 4, 5, 6}).AllConsecutives() ); // true
Console.WriteLine ( (new List<int>() {3, 7, 4, 5, 6}).AllConsecutives() ); // true //as it is not sensitive to list order
Console.WriteLine ( (new List<int>() {2, 3, 4, 7, 6}).AllConsecutives() ); // false //as the five is missing
}
公共静态类myExtension
{
公共静态bool allcontinuences(此IEnumerable targetList)
{
布尔结果=假;
if((targetList!=null)&&(targetList.Any())
{
var ordered=targetList.OrderBy(l=>l);
int first=ordered.first();
结果=有序。全部(项=>项==第一个++);
}
返回结果;
}
}
//测试
void Main()
{
Console.WriteLine((新列表(){2,3,4,5,6}).allConcertives());//true
Console.WriteLine((new List(){3,7,4,5,6}).allconcertives());//true//因为它对列表顺序不敏感
Console.WriteLine((new List(){2,3,4,7,6}).allconcertives());//false//因为缺少五个
}
另一种不需要排序的可能解决方案是使用hashset,在构建hashset时,可以保留min值。这样,运行时间将为O(n)。
这不会考虑重复的值,您可以在构建hashset时添加一个检查,查看hashset是否已经包含该值
HashSet<int> hash = new HashSet<int>();
int minValue = int.MaxValue;
foreach(int i in list)
{
if(minValue > i)
minValue = i;
hash.Add(i);
}
for(int count = 1; count < list.Count; ++count)
{
if(!hash.Contains(++minValue))
return false;
}
return true;
HashSet hash=newhashset();
int minValue=int.MaxValue;
foreach(列表中的int i)
{
如果(最小值>i)
最小值=i;
添加(i);
}
for(int count=1;count
是的,以及?你尝试过什么吗?我尝试过聚合函数,但无法确定种子和累加器应该是什么。聚合只适用于有序列表,依我看,因为你总是访问两个连续的项。他希望它独立于顺序。也许有比先排序然后应用算法更快的方法?对不起,没有看到它应该是顺序独立的列表顺序不敏感,因此,{3,2,1}
在您的示例中会失败,但根据给出的示例,它将通过。使用orderingNo更新示例,它不会在前两个示例上失败,因为比较后预期值会增加。但是是的,第一项可以被跳过
{2,3,4,7,6}通过此操作。OrderBy被延迟,直到其结果被枚举。结果由调用First、调用Count和调用Last来枚举。@DavidB-进行了一些阅读,现在开始。认为它是在第一次通过时排序的,然后就不再排序了。添加了一个“ToList”来停止taht。我是忽略了什么还是值总是空的?@DavidB:谢谢……我失去了希望,因为有人注意到排序是不必要的……现在被标记为答案
list.Sort();
return !list.Skip(1).Where((i, j) => (i != (list[j] + 1))).Any();
HashSet<int> hash = new HashSet<int>();
int minValue = int.MaxValue;
foreach(int i in list)
{
if(minValue > i)
minValue = i;
hash.Add(i);
}
for(int count = 1; count < list.Count; ++count)
{
if(!hash.Contains(++minValue))
return false;
}
return true;