一、题目
二、代码
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;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
三、运行结果