Easiest way to Rotate a List in c#

后端 未结 16 3162
鱼传尺愫
鱼传尺愫 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条回答
  •  Happy的楠姐
    2020-12-01 07:51

    You can use below code for left Rotation.

    List backUpArray = array.ToList();
    
    for (int i = 0; i < array.Length; i++)
    {
        int newLocation = (i + (array.Length - rotationNumber)) % n;
        array[newLocation] = backUpArray[i];
    }
    

提交回复
热议问题