189. Rotate Array----Array----Easy----20160925
问题:
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]
.
思路2:
用一个额外的数组把数组完全复制过去
代码2:
class Solution { public: void rotate(vector<int>& nums, int k) { int len=nums.size(); vector<int> vtcopy(len); if(k<=0 || len==0) return; else { for(int i=0;i<len;i++) { vtcopy[i]=nums[i]; } for(int j=0;j<len;j++) { nums[(j+k)%len]=vtcopy[j]; } } } };