简体   繁体   English

将数组的所有元素向左旋转“ k”位置,但我的代码将其向右移动

[英]Rotating all elements of the array to left by “k” position but my code moves it to the right

Can someone tell how to fix this code so instead of moving the arrays to the right how do i make it move to the left? 有人可以告诉我如何解决此代码,而不是将数组向右移动,如何使它向左移动? Can someone please explain it to me too 有人也可以给我解释一下吗

public class Test {
  public static void main(String[] args) {
    int[] b = {10, 20, 30, 40, 50, 60};
    System.out.println("\n///// Rotate Left k cell example \n////");
    b = copyArray(a, a.length); 
    b = rotateLeft(b,4); //assuming k = 4
    printArray(b); // This Should Print: { 50, 60, 10, 20, 30, 40 };
  }

  // Rotates all the elements of the source array to the right by 'k' positions
  // Returns the updated array
  public static int[] rotateLeft(int[] source, int k) {
    int[] arr = copyArray(source,source.length); //copies all source to a new array
    int[] temp = new int[arr.length];

    for (int i = 0; i < arr.length; i++){
      temp[(i + k) % arr.length] = arr[i];
    }

    for (int i = 0; i < arr.length; i++){
      arr[i] = temp[i];
    }

    return arr; 
  }
}

So this code rotates the elements to the right side. 因此,此代码将元素旋转到右侧。 How should i modify it so that it rotates it to the left side? 我应该如何修改它,使其旋转到左侧?
Output i am getting: 我得到的输出:

{ 30, 40, 50, 60, 10, 20 } {30,40,50,60,10,20}

but my desired output is: 但我想要的输出是:

{ 50, 60, 10, 20, 30, 40 } {50,60,10,20,30,40}

I think you can just change your assignment logic for populating the temp array. 我认为您可以更改用于填充temp数组的分配逻辑。 Change this line: 更改此行:

temp[(i + k) % arr.length] = arr[i];

to this: 对此:

temp[i] = arr[(i + k) % arr.length];

The logic here is that the ith entry in temp[] will equal the entry k steps ahead of it in arr[] . 这里的逻辑是, temp[]中的ith条目将等于arr[] k ith条目。 In other words, temp[] will contain the original elements shifted to the left by k . 换句话说, temp[]将包含左移k的原始元素。 Note that you still need the mod operation to make sure that indexing wraps around the size of the array. 请注意,您仍然需要进行mod操作,以确保索引环绕数组的大小。

Full code: 完整代码:

public static int [] rotateLeft(int [] source, int k){
    int arr[] = copyArray(source, source.length);
    int[] temp = new int[arr.length];
    for (int i=0; i<arr.length; i++) {
        temp[i] = arr[(i + k) % arr.length];
    }
    for (int i=0; i<arr.length; i++) {
        arr[i] = temp[i];
    }

    return arr; 
}

Basically do (i - k) instead of (i + k) . 基本上做(i - k)而不是(i + k) On top of that, remove the copyArray call and the second loop. 最重要的是,删除copyArray调用和第二个循环。 And because the % operator in java behaves the it does, you have to add source.length to (i - k) before actually % ing it: 并且由于Java中的%运算符会表现其行为,因此您必须在实际%之前将source.length添加到(i - k)

public static int [] rotateLeft(int [] source, int k) {
    int arr[] = new int[source.length];

    for (int i = 0; i < arr.length; i++){
        arr[(i - k + source.length) % source.length] = source[i];
    }

    return arr; 
}

Outputting 输出

50 50
60 60
10 10
20 20
30 30
40 40

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM