巧妙地三次旋转。
class Solution {
public:
string LeftRotateString(string str, int n) {
int len = str.length();
if(len == 0)
return str;
if(n <= 0)
return str;
n = n % len;
reverse(str, 0, n - 1);
reverse(str, n, len - 1);
reverse(str, 0, len - 1);
return str;
}
void reverse(string &str, int b, int e){
char temp;
while(b < e)
{
temp = str[e];
str[e] = str[b];
str[b] = temp;
b++;
e--;
}
}
};