4 回答
TA贡献1818条经验 获得超3个赞
我认为这与就地旋转阵列一样有效。适用于左右旋转,具体取决于 的符号rotateBy:
private static void Rotate<T>(T[] array, int rotateBy)
{
rotateBy %= array.Length;
// Nothing to do?
if (rotateBy == 0)
return;
// Normalize it to a right rotation
if (rotateBy < 0)
rotateBy = array.Length + rotateBy;
// Allocate the smallest possible temp array
if (rotateBy > array.Length / 2)
{
T[] temp = new T[array.Length - rotateBy];
Array.Copy(array, 0, temp, 0, array.Length - rotateBy);
Array.Copy(array, array.Length - rotateBy, array, 0, rotateBy);
Array.Copy(temp, 0, array, rotateBy, array.Length - rotateBy);
}
else
{
T[] temp = new T[rotateBy];
Array.Copy(array, array.Length - rotateBy, temp, 0, rotateBy);
Array.Copy(array, 0, array, rotateBy, array.Length - rotateBy);
Array.Copy(temp, 0, array, 0, rotateBy);
}
}
TA贡献1780条经验 获得超3个赞
这是作弊,但 LINQ 解决方案可能是:
var array = Enumerable.Range(0, 100).ToArray();
var shiftBy = 2;
var shifted = array.Skip(shiftBy).Concat(array.Take(shiftBy)).ToArray();
如果您的任务只是以这种转换的方式“查看”数组,为避免创建新数组,请排除末尾.ToArray()并直接迭代IEnumerable<int>。
TA贡献1806条经验 获得超5个赞
使用具有轮班数量大小的旋转缓冲区使其工作。
static void ShiftLeft(int[] array, int shifts)
{
int length = array.Length;
int actualShifts = shifts % length;
if (actualShifts == 0) return;
int[] buffer = new int[actualShifts];
Array.Copy(array, buffer, actualShifts);
int indexAddition = actualShifts - (length % actualShifts);
for (int i = length - 1; i >= 0; i--)
{
int current = array[i];
int bufferIndex = (i + indexAddition) % actualShifts;
array[i] = buffer[bufferIndex];
buffer[bufferIndex] = current;
}
}
编辑:正如@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);
TA贡献1820条经验 获得超2个赞
import java.util.*;
public class ArrayRotation{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter size of array:");
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println("enter elements:");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("enter shifts:");
int d=sc.nextInt();
int arr1[]=new int[d];
for(int i=0;i<d;i++)
arr1[i]=arr[i];
System.out.println("Array after rotating");
for(int i=d;i<n;i++)
System.out.print(arr[i]+" ");
for(int i=0;i<d;i++)
System.out.print(arr1[i]+" ");
}
}
- 4 回答
- 0 关注
- 100 浏览
添加回答
举报