Easiest way to Rotate a List in c#

后端 未结 16 3185
鱼传尺愫
鱼传尺愫 2020-12-01 07:06

Lists say I have a list List {1,2,3,4,5}

Rotate means:

=> {2,3,4,5,1} => {3,4,5,1,2} => {4,5,1,2,3}
16条回答
  •  孤城傲影
    2020-12-01 07:33

    Using Linq,

    List temp = new List();     
    
     public int[] solution(int[] array, int range)
        {
            int tempLength = array.Length - range;
    
            temp = array.Skip(tempLength).ToList();
    
            temp.AddRange(array.Take(array.Length - range).ToList());
    
            return temp.ToArray();
        }
    

提交回复
热议问题