您的位置:首页 > 其它

[Leetcode]-Rotate Array

2015-06-26 13:58 323 查看
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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: