劍指offer中的思路運用三次翻轉。
翻轉字符串的代碼:
void Reserve(string& str,int Begin,int End) {
char temp;
while(Begin<End) {
char temp = str[Begin];
str[Begin] = str[End];
str[End] = temp;
Begin++;
End--;
}
}
實際還有更簡單的方法:
class Solution {
public:
string LeftRotateString(string str, int n) {
int length = str.length();
if(length==0||n>length)
return "";
str += str;
return str.substr(n,length);
}
};