LeetCode 541. Reverse String II

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Example:
Input: s = “abcdefg”, k = 2
Output: “bacdfeg”
Restrictions:
The string consists of lower English letters only.
Length of the given string and k will in the range [1, 10000]

题目大意:给一个字符串s和一个整数k,每2k长度倒置前k个字符串,如果最后剩余的长度小于k则全都倒置,否则如果剩余的长度大于k小于2k,倒置前k个,返回倒置后的字符串~

分析:遍历字符串,步长为2k,每次倒置s.begin() + i~s.begin() + i + k的字符串,如果i + k > s.length()就倒置s.begin() + i~s.begin() + s.length()即可~O(∩_∩)O~

 

❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼

❤ 点击这里 -> 订阅《从放弃C语言到使用C++刷算法的简明教程》by 柳婼

❤ 点击这里 -> 订阅PAT甲级乙级、蓝桥杯、GPLT天梯赛、LeetCode题解离线版