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.
Input: s = “abcdefg”, k = 2
Output: “bacdfeg”
The string consists of lower English letters only.
Length of the given string and k will in the range [1, 10000]


分析:遍历字符串,步长为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题解离线版