码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode]-Rotate Array

时间:2015-06-26 15:03:36      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:leetcode

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);
}

[Leetcode]-Rotate Array

标签:leetcode

原文地址:http://blog.csdn.net/xiabodan/article/details/46649347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!