Rotate Array

时间:2023-03-08 16:31:42

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

 class Solution {
public:
void rotate(int nums[], int n, int k) {
k = k%n;
int* copy = (int*)malloc(sizeof(int)*n);
for(int i=;i<k;++i)
copy[i] = nums[n-k+i];
for(int i=k;i<n;++i)
copy[i] = nums[i-k];
for(int i=;i<n;++i)
nums[i] = copy[i];
free(copy);
}
};