标签:res 左旋转字符串 fse 旋转 offset 行修改 log new size
class Solution {
public:
string LeftRotateString(string str, int n) {
string res;
int len = str.size();
for(int i=n;i<len+n;i++)
{
res.push_back(str[i%len]);
}
return res;
}
};
写得十分简洁了,比当时第一次写这种类型的题的时候进步很大了.
稍加修改就可以通过了
class Solution {
public:
void rotateStr(string &str,int left,int right)
{
while (left < right)
{
char tc = str[left];
str[left] = str[right];
str[right] = tc;
left++;
right--;
}
}
string LeftRotateString(string str, int n) {
int len = str.length();
if(len==0)return string("");
int new_offset = len - n;
rotateStr(str, 0, len-new_offset-1);
rotateStr(str, len-new_offset, len-1);
rotateStr(str, 0, len-1);
return str;
}
};
标签:res 左旋转字符串 fse 旋转 offset 行修改 log new size
原文地址:https://www.cnblogs.com/virgildevil/p/12179588.html