Check if List values are consecutive

前端 未结 12 1816
悲&欢浪女
悲&欢浪女 2021-02-01 05:46
List dansConList = new List();
dansConList[0] = 1;
dansConList[1] = 2;
dansConList[2] = 3;

List dansRandomList = new List

        
12条回答
  •  别那么骄傲
    2021-02-01 06:40

    Here is the another one. It supports {1,2,3,4} and {4,3,2,1} both. It tests sequential number differences equals 1 or -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
    

提交回复
热议问题