您的位置:首页 > 其它

Rotate Array

2015-11-25 19:07 435 查看

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]
.

Note:

Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

解决一:按步移(时间太长没有通过)

解决二:移址(时间短,效率高)

void rotate(int nums[], int n, int k) {

   

    k = k % n;

    int b
;

    if (k == 0) return; 

    int *temp = b; 

    memcpy(temp, nums+(n-k), sizeof(int)*k); 

    memcpy(temp+k, nums, sizeof(int)*(n-k)); 

    memcpy(nums, temp, sizeof(int)*n); 

    

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: