Easiest way to Rotate a List in c#

后端 未结 16 3187
鱼传尺愫
鱼传尺愫 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:36

    public static int[] RightShiftRotation(int[] a, int times) {
      int[] demo = new int[a.Length];
      int d = times,i=0;
      while(d>0) {
        demo[d-1] = a[a.Length - 1 - i]; d = d - 1; i = i + 1;
      }
      for(int j=a.Length-1-times;j>=0;j--) { demo[j + times] = a[j]; }
      return demo;
    }
    

提交回复
热议问题