题目描述
给定一个整数数组 nums
,将数组中的元素向右轮转 k
个位置,其中 k
是非负数。
示例 1:
输入: nums = [1,2,3,4,5,6,7], k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右轮转 1 步: [7,1,2,3,4,5,6]
向右轮转 2 步: [6,7,1,2,3,4,5]
向右轮转 3 步: [5,6,7,1,2,3,4]
示例 2:
输入:nums = [-1,-100,3,99], k = 2
输出:[3,99,-1,-100]
解释:
向右轮转 1 步: [99,-1,-100,3]
向右轮转 2 步: [3,99,-1,-100]
提示:
1 <= nums.length <= 10^5
-2^31 <= nums[i] <= 2^31 - 1
0 <= k <= 10^5
方法一:使用额外的数组
我们可以使用额外的数组来将每个元素放至正确的位置。用 n
表示数组的长度,我们遍历原数组,将原数组下标为 i
的元素放至新数组下标为 (i+k) % n
的位置,最后将新数组拷贝至原数组即可。
代码
// Function to rotate the elements of an array to the right by k steps
void rotate(int* nums, int numsSize, int k) {
// Create a new array to store the rotated elements
int newArr[numsSize];
// Rotate each element to its new position in the new array
for (int i = 0; i < numsSize; ++i) {
// For the first k elements (where i ranges from 0 to k-1 ),
// the expression (i + k) % numsSize will result in values from k to k-1+k ,
// which effectively wraps around the array to the beginning.
// This way, the first k elements in newArr get filled with the elements
// that need to be rotated from the end of the original array.
newArr[(i + k) % numsSize] = nums[i];
}
// Copy the rotated elements back to the original array
for (int i = 0; i < numsSize; ++i) {
nums[i] = newArr[i];
}
}
复杂度分析
时间复杂度: O(n)
,其中 n
为数组的长度。
空间复杂度: O(n)
。
方法二:环状替换
方法一中使用额外数组的原因在于如果我们直接将每个数字放至它最后的位置,这样被放置位置的元素会被覆盖从而丢失。因此,从另一个角度,我们可以将被替换的元素保存在变量 temp
中,从而避免了额外数组的开销。
我们从位置 0
开始,最初令 temp=nums[0]
。根据规则,位置 0
的元素会放至 (0+k) % n
的位置,令 x=(0+k) %n
,此时交换 temp
和 nums[x]
,完成位置 x
的更新。然后,我们考察位置 x
,并交换 temp
和 nums[(x+k) % n]
,从而完成下一个位置的更新。不断进行上述过程,直至回到初始位置 0
。
容易发现,当回到初始位置 0
时,有些数字可能还没有遍历到,此时我们应该从下一个数字开始重复的过程,可是这个时候怎么才算遍历结束呢?我们不妨先考虑这样一个问题:从 0
开始不断遍历,最终回到起点 0
的过程中,我们遍历了多少个元素?
我们用下面的例子更具体地说明这个过程:
nums = [1, 2, 3, 4, 5, 6]
k = 2
也可以使用另外一种方式完成代码:使用单独的变量 count
跟踪当前已经访问的元素数量,当 count=n
时,结束遍历过程。
代码
Here are comments added to the provided code:
// Function to calculate the greatest common divisor of two numbers using Euclidean algorithm
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
// Function to swap two integer values
void swap(int* a, int* b) {
int t = *a;
*a = *b, *b = t;
}
// Function to rotate the elements of an array in-place to the right by k steps
void rotate(int* nums, int numsSize, int k) {
// Normalize k to be within the range of array size
k = k % numsSize;
// Find the count of elements to be rotated in each cycle
int count = gcd(k, numsSize);
// Perform rotation in cycles
for (int start = 0; start < count; ++start) {
int current = start;
int prev = nums[start];
do {
int next = (current + k) % numsSize;
swap(&nums[next], &prev);
current = next;
} while (start != current); // Continue the cycle until reaching the starting point
}
}
复杂度分析
时间复杂度:O(n)
,其中 n
为数组的长度。每个元素只会被遍历一次。
空间复杂度:O(1)
。我们只需常数空间存放若干变量。
方法三:数组翻转
该方法基于如下的事实:当我们将数组的元素向右移动 k
次后,尾部 k % n
个元素会移动至数组头部,其余元素向后移动 k % n
个位置。
该方法为数组的翻转:我们可以先将所有元素翻转,这样尾部的 k % n
个元素就被移至数组头部,然后我们再翻转 [0,k % n−1]
区间的元素和 [k % n,n−1]
区间的元素即能得到最后的答案。
我们以 n=7,k=3
为例进行如下展示:
代码
// Function to swap two integer values
void swap(int* a, int* b) {
int t = *a;
*a = *b, *b = t;
}
// Function to reverse the elements in a subarray between start and end indices
void reverse(int* nums, int start, int end) {
while (start < end) {
swap(&nums[start], &nums[end]); // Swap elements at start and end indices
start += 1; // Move start index forward
end -= 1; // Move end index backward
}
}
// Function to rotate the elements of an array in-place to the right by k steps
void rotate(int* nums, int numsSize, int k) {
k %= numsSize; // Normalize k to be within the range of array size
// Perform rotation in three steps: reverse the entire array, then reverse the first k elements and the remaining elements separately
reverse(nums, 0, numsSize - 1); // Reverse the entire array
reverse(nums, 0, k - 1); // Reverse the first k elements
reverse(nums, k, numsSize - 1); // Reverse the remaining elements
}
复杂度分析
时间复杂度:O(n)
,其中 n
为数组的长度。每个元素被翻转两次,一共 n
个元素,因此总时间复杂度为 O(2n)=O(n)
。
空间复杂度:O(1)
。
作者:力扣官方题解
链接:https://leetcode.cn/problems/rotate-array/solutions/551039/xuan-zhuan-shu-zu-by-leetcode-solution-nipk/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。