参考
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
class Solution {
public void rotate(int[] nums, int k)
{
int n = nums.length;
k = k % n;
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}
// 翻转数组 nums 的区间 [l,r]
private void reverse(int[] nums, int l, int r)
{
while (l < r)
{
int t = nums[l];
nums[l] = nums[r];
nums[r] = t;
l++;
r--;
}
}
}