问题1:字符串循环左移
时间复杂度O(n),空间复杂度O(1)。
两次翻转。
void ReverseString(char *s, int from, int to){
while(from < to){
char t = s[from];
s[from++] = s[to];
s[to--] = t;
}
}
void LeftRotateString(char *s, int n, int m){
m %= n; //因为m >= n时,左移的结果以n为周期重复出现。
ReverseString(char *s, int 0, int m - 1);
ReverseString(char *s, int m, int n - 1);
ReverseString(char *s, int 0, int n - 1);
}