Code Capriccio - String - Reverse String II

Reverse string II

This section corresponds to Code Random Notes: Code Random Notes , Explanatory Video: Advanced String Operations! | LeetCode: 541. Reverse String II_bilibili_bilibili

exercise

Question link: 541. Reverse String II - LeetCode

Given a string s and an integer k, every time 2k characters are counted from the beginning of the string, reverse the first k characters of the 2k characters.

If there are less than k characters remaining, reverse all remaining characters.
If the remaining characters are less than 2k but greater than or equal to k, then the first k characters are reversed and the remaining characters are left intact.

示例 1:
输入:s = "abcdefg", k = 2
输出:"bacdfeg"

My solution

Not optimal, just a personal record

Although I passed it, the writing was a bit more troublesome than the official solution, but I still recorded it.

The meaning of the question is to reverse the first k characters every 2k characters. Then you can use two pointers left and right to represent the left and right boundaries of each reversal. And each time you reverse, 2k must be added to left and right to jump to The next reversal range. When it is less than a multiple of 2k, the remaining part will also be reversed once, so the total number of reversals is. size / (2 * k) + 1When it is exactly a multiple of 2k, there will be one more reversal, but only the last character is reversed, which is equivalent to nothing. Change. At the same time, left and right jump 2k each time. When right jumps outside size, let right=size solve the problem of reversing all characters when the remaining characters are less than k.

class Solution {
    
    
   public:
    string reverseStr(string s, int k) {
    
    
        int size = s.length();
        // 如果长度小于等于k则反转s并返回
        if(size <=k){
    
    
            reverse(s.begin(),s.end());
            return s;
        }
        int left = 0, right = k; // 每次反转的左右边界
        int nums = size / (2 * k) + 1; // 反转次数
        for (int i = 0; i < nums; i++) {
    
    
            reverse(s.begin() + left, s.begin() + right);
            left += 2 * k;
            right += 2 * k;
            // 如果右边界大于数组长度,则让right等于size,以反转剩下的字符
            if (right > size) {
    
    
                right = size;
            }
        }
        return s;
    }
};
  • Time complexity: O( nnn ). The number of loops is at most n/(2k) + 1, so the total time complexity is O(n)
  • Space complexity: O( 1 11 ). Only extra space at the constant level is used, and only a few variables are needed to store temporary values, so the space complexity is O(1)

LeetCode official solution

In my solution, the for loop just repeats the number of inversions, while in LeetCode's solution, i +=2k each time. Different from the commonly used i++, every 2k in this question is a cycle, so it is used i+=2kto calculate the number of cycles. Each loop i is at the left boundary of the reversal interval, and the right boundary is i+k. However, if the remaining characters are less than k, all the remaining characters must be reversed. When this happens, i+k is on the right side of size, and you only need to change the right boundary to size. So the right boundary every time ismin(i+k,size)

class Solution {
    
    
public:
    string reverseStr(string s, int k) {
    
    
        int size = s.length();
        for (int i = 0; i < size; i += 2 * k) {
    
    
            reverse(s.begin() + i, s.begin() + min(i + k, size));
        }
        return s;
    }
};
  • Time complexity: O( nnn ). Where n is the length of the input string, and the maximum number of loops is n/(2k), so the total time complexity is O(n)
  • Space complexity: O( 1 11 ). Only extra space at the constant level is used, and only a few variables are needed to store temporary values, so the space complexity is O(1)

Guess you like

Origin blog.csdn.net/zss192/article/details/129837875