自己想的一个新的写法,如果不排除length=0的情况,下面那个while是死循环
class Solution { public: string LeftRotateString(string str, int n) { int length = str.length(); if(length == 0) return str; while(length <= n) n = n - length; Rotate(str,0,length-1); Rotate(str,0,length-1-n); Rotate(str,length-n,length-1); return str; } void Rotate(string &str,int start,int end){ for(int i = start;i <= (start + end)/2;i++){ char tmp = str[i]; str[i] = str[start+end-i]; str[start+end-i] = tmp; } } };