如何向右旋转数组

时间:2016-02-03 06:09:22

标签: java arrays rotation

我已经编写了一个程序来移动一个int数组,但找不到合适的方法。如果你有任何想法如何旋转"你可以看看我的代码和评论吗?我的数组基于空格数(int x),因为它目前只向左移动。感谢

public void makeRight(int x) {
   int[] anArray = {0, 1, 2, 3, 4, 5};
   int counter = 0;
   while (counter < x) {
        int temp = anArray[0];
        for (int i = 0; i < anArray.length - 1; i++) {
            anArray[i] = anArray[i + 1];
         }

         anArray[anArray.length - 1] = temp;
         counter++;
  }
  for (int i = 0; i < anArray.length; i++){
      System.out.print(anArray[i] + " ");
  }
}

8 个答案:

答案 0 :(得分:1)

向右旋转数组

public void makeRight( int x )
{
    int[] anArray =
    { 0, 1, 2, 3, 4, 5 };
    int counter = 0;
    while ( counter < x )
    {
        int temp = anArray[anArray.length - 1];
        for ( int i = anArray.length - 1; i > 0; i-- )
        {
            anArray[i] = anArray[i - 1];
        }
        anArray[0] = temp;
        counter++;
    }
    for ( int i = 0; i < anArray.length; i++ )
    {
        System.out.print( anArray[i] + " " );
    }
}

答案 1 :(得分:0)

while (counter < x) {
    int temp = anArray[anArray.length - 1];
    for (int i = anArray.length - 1; i > 0; i--) {
        anArray[i] = anArray[i - 1];
    }
    anArray[0] = temp;
    counter++;
}

答案 2 :(得分:0)

在我看来,基本上你已经完成了大部分旋转阵列的部分(右)。 只是那个概念

  

anArray[i] = secondArray[(i + x) % anArray.length];

  

anArray[(i + x) % anArray.length] = secondArray[i];

有点不同。

会有像这样的东西

    int[] anArray = {0, 1, 2, 3, 4, 5};
    //int counter = 0;
    //int x = 2;
    int[] secondArray = new int[anArray.length];

    for (int i = 0; i < anArray.length; i++) {
        secondArray[(i + x) % anArray.length] = anArray[i];
    }

    for (int i = 0; i < secondArray.length; i++){
        System.out.print(secondArray[i] + " ");
    }

至于&#34;%&#34;工作,Codility - CyclicRotation这个链接应该有一个明确的解释。

答案 3 :(得分:0)

这样的事情应该有效

    private void shiftArrayRight() {
        int endElementvalue = element[element - 1];
        int[] startElements = Arrays.copyOfRange(element, 0 , element.length - 1);
        element[0] = endElementvalue;
        for(int i = 0, x = 1; i < startElements.length; i++, x++) {
            element[x] = startElements[i];
        }
        System.out.println(Arrays.toString(element);
    }

答案 4 :(得分:0)

以下功能可以帮助您

public static void rightRotateArray(int[] a, int requiredIterations) {
  // right-rotate [a] by k moves
  // totalActiveIterations  by MOD 
  // => because every n(a.length) rotations ==> we receive the same array
  int totalActiveIterations = requiredIterations % a.length; 
  for (int i = 0; i < totalActiveIterations; i++) {
   // make lastElement as BKP temp
   int temp = a[a.length - 1];
   // make other elements => each one equal previous one [starting by lastElement]
   for (int j = (a.length - 1); j >= 1; j--) {
    a[j] = a[j - 1];
   }
   // make 1stElement equal to (BKP as temp = lastElement)
   a[0] = temp;
  }
 }

答案 5 :(得分:0)

其他答案仅仅是代码转储,解释为零。这是我想出的一种算法:

我们将阵列旋转到位。请注意,每个元素的目标位置均由(index + k) modulo size给出。对于范围0k - 1,只要目标位置大于当前位置,我们就会递归交换每个元素与目标位置中的元素。这是因为由于我们是从低到高逐步递增的,所以较小的目标索引表明相应的元素已经被交换。

Example:
Rotate [1, 2, 3, 4, 5, 6] by 3

Index to target index:
0 to 3
1 to 4
2 to 5
3 to 0
4 to 1
5 to 2

swap(0, 3) => [4, 2, 3, 1, 5, 6]
swap(0, 0) => return
swap(1, 4) => [4, 5, 3, 1, 2, 6]
swap(1, 1) => return
swap(2, 5) => [4, 2, 6, 1, 2, 3]
swap(2, 2) => return

Done!

Another example:
Rotate [2, 3, 4, 1] by 1

Index to target index:
0 to 1
1 to 2
2 to 3
3 to 0

swap(0, 1) => [3, 2, 4, 1]
swap(0, 2) => [4, 2, 3, 1]
swap(0, 3) => [1, 2, 3, 4]
swap(3, 0) => return

Done!

代码:

static void rotateRight(int[] xs, int k) {
        swap(0, 0, xs, k);
}

private static void swap(int original, int current, int[] xs, int k) {
    int target = (original + k) % xs.length;

    if (target > current) {
        int tmp = xs[current];
        xs[current] = xs[target];
        xs[target] = tmp;

        swap(target, current, xs, k);
    }
}

答案 6 :(得分:0)

public static List<int> rotateLeft(int d, List<int> arr)
{
    int listSize = arr.Count();
    int[] newArr = new int[listSize];
    
    for(int oldIndex=0; oldIndex< listSize; oldIndex++)
    {
        int newIndex = (oldIndex + (listSize - d))% listSize;
        newArr[newIndex] = arr[oldIndex];
    }
    List<int> newList = new List<int>(newArr);
    return newList;
}

答案 7 :(得分:-1)

只需像这样更改代码

public void makeRight(int x) {
int[] anArray = {0, 1, 2, 3, 4, 5};
int counter = 0;
while(counter< x){
       int temp = anArray[anArray.length - 1];
        for (int i = anArray.length - 1; i > 0; i--) {
            anArray[i] = anArray[i - 1];
        }

        anArray[0] = temp;
        counter++;
 }
for (int i = 0; i < anArray.length; i++)
System.out.print(anArray[i] + " ");
}