一、题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e1722c9b5dc10f30d5df900036e39692.png)
二、代码
class Solution {
public:
string reverseStr(string s, int k) {
std::cout<<"s "<<s<<std::endl;
std::cout<<"k "<<k<<std::endl;
int s_size=s.size();
int over_time=s_size/(2*k);
int remain_length=s_size%(2*k);
int i,j;
int count=0;
string::iterator it_over_begin=s.begin();
string::iterator it_over_end=s.begin();
if(k==1) return s;
while(it_over_end!=s.end()-remain_length)
{
it_over_end++;
count=count+1;
if(count%k==0&&count%(2*k)!=0)
{
reverse(it_over_begin,it_over_end);
std::cout<<"alive"<<std::endl;
}
if(count%(2*k)==0)
{
it_over_begin=it_over_end;
}
}
for(i=0;i<s.size();i++)
{
std::cout<<" "<<s[i];
}
std::cout<<std::endl;
if(remain_length<=k) reverse(it_over_end,s.end());
if(remain_length>k) reverse(it_over_end,it_over_end+k);
return s;
}
};
三、运行结果![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/685904bf714664baaed7dc8eac69c0c0.png)