void inversion(std::string& str, int start, int end)
{
char temp;
while (start < end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
++start;
--end;
}
}
void shift1(std::string& str, int n)
{
int len = str.length();
if (len <= 0) return;
n = n % len;
inversion(str, 0, n - 1);//反转前n半部分
inversion(str, n, len - 1);//反转后半部分
inversion(str, 0, len - 1);//整体反转
}
反转[start end]之间字符串,如abcde->cdeab
最新推荐文章于 2023-09-21 16:15:24 发布