📜  将包含 n 个元素的数组向右旋转 k 步 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:49.815000             🧑  作者: Mango

代码示例1
public void rotate(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }
    public void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }