题目链接
string reverse(string s1)
{string s2;string::reverse_iterator rit = s1.rbegin();while (rit != s1.rend()){s2 += *rit;rit++;}return s2;
}
class Solution {
public:string reverseStr(string s, int k) {string s1;int i = 0;//标记字符串下标int j = 0;int length =s.length();for (i = 0; i < length; i+=2*k){if (i+2*k<length){string tmp = s.substr(i, k);//字符串s从i号下标位置取k个字符tmp = reverse(tmp);s1 += tmp;s1 += s.substr(i + k, k);}else if (i + 2 * k >= length&&i+k<length){string tmp = s.substr(i, k);//字符串s从i号下标位置取k个字符tmp = reverse(tmp);s1 += tmp;s1 += s.substr(i + k, length - (i + k));}else//i+k>length{string tmp = s.substr(i, length);//字符串s从i号下标位置取k个字符tmp = reverse(tmp);s1 += tmp;}}return s1;}
};