1 void reverse(char* str, int begin, int end) 2 { 3 char temp; 4 for( ; begin < end; begin++) 5 { 6 temp = str[end]; 7 str[end] = str[begin]; 8 str[begin] = temp; 9 } 10 } 11 12 void left_shift(char *str, int n, int k) 13 { 14 k=k% N; 15 reverse(str, 0, k -1); 16 reverse(str, k , n - 1); 17 reverse(str, 0, n - 1); 18 }
先把前n个字符串反转,再把后面的反转。最后再把整个字符串反转
时间: 2024-11-03 19:48:21