标签:
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]
.
1 class Solution { 2 public: 3 void rotate(int nums[], int n, int k) { 4 k = k%n; 5 int* copy = (int*)malloc(sizeof(int)*n); 6 for(int i=0;i<k;++i) 7 copy[i] = nums[n-k+i]; 8 for(int i=k;i<n;++i) 9 copy[i] = nums[i-k]; 10 for(int i=0;i<n;++i) 11 nums[i] = copy[i]; 12 free(copy); 13 } 14 };
标签:
原文地址:http://www.cnblogs.com/ittinybird/p/4373972.html