三步反转法:先将前m个字符反转,再反转后面(n-m)个字符, 最后反转整个字符串。code
例如:abcdef ,n=6, m=3,cba + fed =>defabc字符串
void ReverseString( char* s, int from, int to){ while( from < to){ char temp = s[from]; s[from++] = s[to]; s[to--] = temp; } }
m==n,m%=n => m==0, while
m<n , m==mco
void LeftRotate( char* s, int n, int m){ if(m>n) ; else { m%=n; ReverseString( s, 0, m-1); ReverseString( s, m, n-1); ReverseString( s, 0, n-1); } }