检查List 值是否连续

List dansConList = new List(); dansConList[0] = 1; dansConList[1] = 2; dansConList[2] = 3; List dansRandomList = new List(); dansRandomList[0] = 1; dansRandomList[1] = 2; dansRandomList[2] = 4; 

我需要一个方法,在评估上面的列表时,将为dansRandomList返回false对于dansRandomList返回true ,基于事实dansConList在其值中有一个连续的数字序列,而dansRandomList则没有(缺少值3)。

如果可能,最好使用LINQ。

我试过的:

  • 为了达到最终结果,我使用了for循环并与’i’(循环计数器)进行比较以评估值,但如上所述,我想使用LINQ。

单行,只迭代直到第一个非连续元素:

 bool isConsecutive = !myIntList.Select((i,j) => ij).Distinct().Skip(1).Any(); 

更新:几个如何工作的例子:

 Input is { 5, 6, 7, 8 } Select yields { (5-0=)5, (6-1=)5, (7-2=)5, (8-3=)5 } Distinct yields { 5, (5 not distinct, 5 not distinct, 5 not distinct) } Skip yields { (5 skipped, nothing left) } Any returns false 
 Input is { 1, 2, 6, 7 } Select yields { (1-0=)1, (2-1=)1, (6-2=)4, (7-3=)4 } * Distinct yields { 1, (1 not distinct,) 4, (4 not distinct) } * Skip yields { (1 skipped,) 4 } Any returns true 

* Select不会产生第二个4而Distinct不会检查它,因为Any会在找到第一个4之后停止。

 var result = list .Zip(list.Skip(1), (l, r) => l + 1 == r) .All(t => t); 
 var min = list.Min(); var max = list.Max(); var all = Enumerable.Range(min, max - min + 1); return list.SequenceEqual(all); 

您可以使用此扩展方法:

 public static bool IsConsecutive(this IEnumerable ints ) { //if (!ints.Any()) // return true; //Is empty consecutive? // I think I prefer exception for empty list but I guess it depends int start = ints.First(); return !ints.Where((x, i) => x != i+start).Any(); } 

像这样用它:

 [Test] public void ConsecutiveTest() { var ints = new List {1, 2, 4}; bool isConsecutive = ints.IsConsecutive(); } 

扩展方法:

 public static bool IsConsecutive(this IEnumerable myList) { return myList.SequenceEqual(Enumerable.Range(myList.First(), myList.Last())); } 

用途:

 bool isConsecutive = dansRandomList.IsConsecutive(); 

这是另一个。 它支持{1,2,3,4}和{4,3,2,1}。 它测试顺序数差异等于1或-1。

 Function IsConsecutive(ints As IEnumerable(Of Integer)) As Boolean If ints.Count > 1 Then Return Enumerable.Range(0, ints.Count - 1). All(Function(r) ints(r) + 1 = ints(r + 1) OrElse ints(r) - 1 = ints(r + 1)) End If Return False End Function 

这是一个使用Aggregate函数的扩展方法。

 public static bool IsConsecutive(this List value){ return value.OrderByDescending(c => c) .Select(c => c.ToString()) .Aggregate((current, item) => (item.ToInt() - current.ToInt() == -1) ? item : "" ) .Any(); } 

用法:

 var consecutive = new List(){1,2,3,4}.IsConsecutive(); //true var unorderedConsecutive = new List(){1,4,3,2}.IsConsecutive(); //true var notConsecutive = new List(){1,5,3,4}.IsConsecutive(); //false 

它仅适用于唯一列表。

 List dansConList = new List(); dansConList.Add(7); dansConList.Add(8); dansConList.Add(9); bool b = (dansConList.Min() + dansConList.Max())*((decimal)dansConList.Count())/2.0m == dansConList.Sum(); 

警告:如果为空,则返回true。

 var list = new int[] {-1,0,1,2,3}; var isConsecutive = list.Select((n,index) => n == index+list.ElementAt(0)).All (n => n); 

这是一个C版本代码,我认为很容易用基于逻辑的其他语言重写它。

 int isConsecutive(int *array, int length) { int i = 1; for (; i < length; i++) { if (array[i] != array[i - 1] + 1) return 0; //which means false and it's not a consecutive list } return 1; }