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:
1 | Input: s = "abcdefg", k = 2 |
Restrictions:
- The string consists of lower English letters only.
- Length of the given string and k will in the range [1, 10000]
解题思路
题目是给出一个字符串,再给出一个值k,让我们将k个数字为一组进行翻转。但是是将0-k,2k-3k,…。就是说将字符串K个数字分为一组,第一组翻转,第二组保持,第三组翻转,第四组保持这样。用一个循环就可以解决了,很简单的题。
解题代码【.CPP】
1 | class Solution { |