题目链接:LeetCode 189—Rotate Array
题目大意:将给定顺序数组(1–n)循环右移k位,如:
n=7,k=3
则将[1,2,3,4,5,6,7] –> [5,6,7,1,2,3,4]
实现代码如下:
public class Problem189 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void rotate(int[] nums, int k) {
int start = 0;
while (start < nums.length - 1) {
k %= nums.length - start;
if (k == 0) break;
for (int i = nums.length - k; i < nums.length; i++) {
int tmp = nums[start];
nums[start++] = nums[i];
nums[i] = tmp;
}
}
}
}