[Leetcode]-Rotate Array

it2025-05-28  5

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]. 将固定数组循环右移k步 注意:当k>numsSize的时候k = k % numsSize

void rotate(int* nums, int numsSize, int k) { k = k%numsSize;//very important ,when k>numsSize ! int *tem = (int*)malloc(sizeof(int)*numsSize); int i = numsSize - k; memcpy(tem,nums+i,k*4); memcpy(tem+k,nums,i*4); memcpy(nums,tem,numsSize*4); free(tem); }

转载于:https://www.cnblogs.com/bhlsheji/p/5374827.html

相关资源:数据结构—成绩单生成器
最新回复(0)