import java.util.*; public class Solution { /** * 旋转数组 * @param n int整型 数组长度 * @param m int整型 右移距离 * @param a int整型一维数组 给定数组 * @return int整型一维数组 */ public int[] solve (int n, int m, int[] a) { if(n <= 1 || m == 0 || m % n == 0) return a ; m = m % n ; //[1,2,3,4,5] m=2 reverse(a , 0 , n - 1) ;//[5,4,3,2,1] reverse(a , 0 , m - 1) ;//[4,5,3,2,1] reverse(a , m , n - 1) ;//[4,5,1,2,3] return a ; } public void reverse(int[] arr , int s , int e) { int i = s ; int j = e ; while(i < j) { int t = arr[i] ; arr[i] = arr[j] ; arr[j] = t ; i ++ ; j -- ; } } }