题解 | #旋转数组#
旋转数组
http://www.nowcoder.com/practice/e19927a8fd5d477794dac67096862042
三次交换
public class Solution { /** * 旋转数组 * @param n int整型 数组长度 * @param m int整型 右移距离 * @param a int整型一维数组 给定数组 * @return int整型一维数组 */ public int[] solve (int n, int m, int[] a) { m = m%n; // write code here reverse(a,0,n - 1); reverse(a,0,m - 1); reverse(a, m, n - 1); return a; } public void reverse(int[] nums, int start, int end){ while(start < end){ swap(nums, start++, end--); } } public void swap(int[] nums, int a, int b){ int temp = nums[a]; nums[a] = nums[b]; nums[b] = temp; } }