为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
侃侃尔雅 2022-11-13 14:56:17
我尝试在大小为 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();它正在工作,但它没有通过一些数组中有大量数字的测试,我因超时错误而被终止;有没有办法在一个循环中实现左旋转?
查看完整描述

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);  

    }

}


查看完整回答
反对 回复 2022-11-13
?
Helenr

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>。


查看完整回答
反对 回复 2022-11-13
?
忽然笑

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);


查看完整回答
反对 回复 2022-11-13
?
DIEA

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]+" ");

}

}


查看完整回答
反对 回复 2022-11-13
  • 4 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信