想了半天最后还是用了暴力解法,总觉得应该还有更巧妙的算法。
class Solution {
public:
void rotate(int nums[], int n, int k) {
int temp;
int i,j;
int count;
k=n-k%n;//将向右旋转变成向左旋转,但注意k的值有可能比n大
if(k==n)
return;
else{
count=0;
while(count<k)
{
temp=nums[0];
for(i=1;i<n;i++)
{
nums[i-1]=nums[i];
}
nums[n-1]=temp;
count++;
}
}
}
};