LeetCode: Rotate Array

LeetCode: Rotate Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

/**
* Created by hzhou on 4/22/15. [email protected]
*/
public class RotateArray {
public void rotate(int[] nums, int k) {
if (nums == null || nums.length < 2) {
return;
}
k = k % nums.length;
int[] tmp = new int[k];
System.arraycopy(nums, nums.length - k, tmp, 0, k);
System.arraycopy(nums, 0, nums, k, nums.length - k);
System.arraycopy(tmp, 0, nums, 0, k);
}
}