将N个字符的数组,循环右移K位。时间复杂度O(N)
比如:str[]=”abcde123”,右移3位
1、旋转abcde->edcba
2、旋转123->321
3、整体旋转abcde123->123abcde
void reverse(char *str, int left, int right)
{
while (left<right)
{
char tmp = str[right];
str[left] = str[right];
str[right] = tmp;
left++;
right--;
}
}
void rightMove(char *str, int length, int k)
{
k = k%length;
//旋转前k个字符
reverse(str, 0, length - k - 1);
//旋转剩余字符
reverse(str, length - k, length - 1);
//整体旋转
reverse(str, 0, length - 1);
}