题解 | 左旋转字符串
左旋转字符串
https://www.nowcoder.com/practice/12d959b108cb42b1ab72cef4d36af5ec
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str string字符串 * @param n int整型 * @return string字符串 */ void Swap(char *x, char *y, int width) { char t = 0; while (width--) { t = *x; *x++ = *y; *y++ = t; } } #include <string.h> char* LeftRotateString(char* str, int n ) { int i=0,j=0,sz=strlen(str); if(sz==0) return ""; n=n%sz; for(;i<n;i++) { for(j=0;j<sz-1;j++) Swap(str+j,str+j+1,1); } return str; }