package array;

/**

  • 移动数组

  • https://leetcode-cn.com/problems/rotate-array/

  • /
    public class RotateArray {

    public static void main(String[] args) {

      int[] array = new int[]{1, 2, 3, 4, 5, 6, 7};
    
      int[] result = rotateArray(array.length, 3, array);

    }

    /**

    • 旋转数组

    • @param n int整型 数组长度

    • @param m int整型 右移距离

    • @param a int整型一维数组 给定数组

    • @return int整型一维数组

    • /
      public static int[] rotateArray(int n, int m, int[] a) {

      //建立辅助数组存储
      int[] newArr = new int[n];
      for (int i = 0; i < n; ++i) {

        newArr[(i + m) % n] = a[i]; //理解这里

      }
      System.arraycopy(newArr, 0, a, 0, n);
      return newArr;
      }
      }