对大小为 n 的数组进行左旋转操作移位

我尝试在大小为 n 的数组上实现左旋转。例如,我有


数组 = {1,2,3,4,5};


我有几个班次:


班次 = 2;


处理数组后,它必须如下所示:


数组 = {3,4,5,1,2};


我用两个for循环实现了它:


var array = new int[]{1,2,3,4,5};

var shifts =3;

var temp = 0;

for(var j = 0; j < shifts; j++){

     temp = array[0];

     for(var i = 0; i < array.Length -1; i++){

         array[i] = array[i + 1];

     }

     array[array.Length-1] = temp;

}

for(var i =0 ; i< array.Length; i++){

    System.Console.Write(array[i]+ " ");

}

 Console.Read();

它正在工作,但它没有通过一些数组中有大量数字的测试,我因超时错误而被终止;


有没有办法在一个循环中实现左旋转?


侃侃尔雅
浏览 101回答 4
4回答

泛舟湖上清波郎朗

我认为这与就地旋转阵列一样有效。适用于左右旋转,具体取决于 的符号rotateBy:private static void Rotate<T>(T[] array, int rotateBy){&nbsp; &nbsp; rotateBy %= array.Length;&nbsp; &nbsp; // Nothing to do?&nbsp; &nbsp; if (rotateBy == 0)&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; // Normalize it to a right rotation&nbsp; &nbsp; if (rotateBy < 0)&nbsp; &nbsp; &nbsp; &nbsp; rotateBy = array.Length + rotateBy;&nbsp; &nbsp; // Allocate the smallest possible temp array&nbsp; &nbsp; if (rotateBy > array.Length / 2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; T[] temp = new T[array.Length - rotateBy];&nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(array, 0, temp, 0, array.Length - rotateBy);&nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(array, array.Length - rotateBy, array, 0, rotateBy);&nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(temp, 0, array, rotateBy, array.Length - rotateBy);&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; T[] temp = new T[rotateBy];&nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(array, array.Length - rotateBy, temp, 0, rotateBy);&nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(array, 0, array, rotateBy, array.Length - rotateBy);&nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(temp, 0, array, 0, rotateBy);&nbsp;&nbsp;&nbsp; &nbsp; }}

Helenr

这是作弊,但 LINQ 解决方案可能是:var array = Enumerable.Range(0, 100).ToArray();var shiftBy = 2;var shifted = array.Skip(shiftBy).Concat(array.Take(shiftBy)).ToArray();如果您的任务只是以这种转换的方式“查看”数组,为避免创建新数组,请排除末尾.ToArray()并直接迭代IEnumerable<int>。

忽然笑

使用具有轮班数量大小的旋转缓冲区使其工作。static void ShiftLeft(int[] array, int shifts){&nbsp; &nbsp; int length = array.Length;&nbsp; &nbsp; int actualShifts = shifts % length;&nbsp; &nbsp; if (actualShifts == 0) return;&nbsp; &nbsp; int[] buffer = new int[actualShifts];&nbsp; &nbsp; Array.Copy(array, buffer, actualShifts);&nbsp; &nbsp; int indexAddition = actualShifts - (length % actualShifts);&nbsp; &nbsp; for (int i = length - 1; i >= 0; i--)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int current = array[i];&nbsp; &nbsp; &nbsp; &nbsp; int bufferIndex = (i + indexAddition) % actualShifts;&nbsp; &nbsp; &nbsp; &nbsp; array[i] = buffer[bufferIndex];&nbsp; &nbsp; &nbsp; &nbsp; buffer[bufferIndex] = current;&nbsp; &nbsp; }}编辑:正如@canton7 所指出的,循环缓冲区增加了不必要的复杂性。下面应该做,基本上是@canton7 的答案,尽管@canton7 由于缓冲区较小,他的答案仍然更有效:int length = array.Length;int actualShifts = shifts % length;if (actualShifts == 0) return;int[] buffer = new int[actualShifts];Array.Copy(array, buffer, actualShifts);Array.Copy(array, actualShifts, array, 0, length - actualShifts);Array.Copy(buffer, 0, array, length - actualShifts, actualShifts);

DIEA

import java.util.*;&nbsp;public class ArrayRotation{&nbsp; &nbsp; &nbsp;public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Scanner sc=new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("enter size of array:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int n=sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int arr[]=new int[n];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("enter elements:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=0;i<n;i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr[i]=sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("enter shifts:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int d=sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int arr1[]=new int[d];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i=0;i<d;i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr1[i]=arr[i];&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Array after rotating");&nbsp; &nbsp; &nbsp; &nbsp; for(int i=d;i<n;i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(arr[i]+" ");&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<d;i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print(arr1[i]+" ");}}
打开App,查看更多内容
随时随地看视频慕课网APP