[LeetCode] 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:

  1. The string consists of lower English letters only.
  2. Length of the given string and k will in the range [1, 10000]

对字符串进行反转,规则是:每隔k个反转k个字母。如果剩下的字母不满足k则,则将剩下的字母全部反转。取t = n / k,将字符串分成k份,每隔一份反转一次。

class Solution {
public:
    string reverseStr(string s, int k) {
        int n = s.size(), t = n / k;
        for (int i = 0; i <= t; i++) {
            if (i % 2 == 0) {
                if (i * k + k < n)
                    reverse(s.begin() + i * k, s.begin() + i * k + k);
                else
                    reverse(s.begin() + i * k, s.end());
            }
        }
        return s;
    }
};
// 6 ms

相关题目:Reverse String

时间: 2024-10-13 04:35:34

[LeetCode] Reverse String II的相关文章

[LeetCode] 344 Reverse String &amp; 541 Reverse String II

原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&解法: 1.Reverse String: Write a function that takes a string as input and returns

[LeetCode] Reverse String 翻转字符串

Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 这道题没什么难度,直接从两头往中间走,同时交换两边的字符即可,参见代码如下: 解法一: class Solution { public: string reverseString(string s) { int left =

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

[Algorithm] Reverse String II

给定一个字符串,要求把字符串前面的若干字符移动到字符串尾部. 解法一:蛮力法 首先想考虑将一个字符移到尾部的方法.代码如下: void LeftShiftOne(char* s, int n) { char t = s[0]; for (int i = 1; i != n; i++) s[i - 1] = s[i]; s[n - 1] = t; } 如果要移动m个字符串,则依次对函数LeftShiftOne执行m次即可.代码如下: void LeftRotateString(char* s, i

LeetCode Reverse Words in a String III

原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Inpu

*LeetCode--Reverse String II (自己的做法很笨)

Reverse String II 自己的解法: 就是用一个StringBuffer来进行字符串的接收,利用一个指针来指示当前是哪一个k部分,当 i + k < s.length() 时,证明从 i 到 i + k - 1部分是可以进行反转的,而从i + k 部分到 i + 2 * k部分是直接进行拼接,利用条件 j < s.length()即可进行限制,不过对于最后剩余的一小部分如果是需要反转的,那么就不能使用上面反转时使用的条件 : 从j = i + k - 1 ,而是从 j = s.le

[LeetCode] Reverse Words in a String II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t

[LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le