代码随想录算法训练营第八天|Leetcode344.反转字符串 Leetcode541.反转字符串II Leetocde剑指offer05.替换空格 Leetcode151.反转字符串中的单词 等

Leetcode344.反转字符串

题目链接

class Solution {
public:
    void reverseString(vector& s) {
        int left = 0, right = s.size() - 1;
        while (left < right) {
            char tmp = s[left];
            s[left] = s[right];
            s[right] = tmp;
            left++;
            right--;
        }
    }
};

Leetcode.541反转字符串II

题目链接

体会如何显化问题中的共性部分

class Solution {
public:
    string reverseStr(string s, int k) {
        for (int i = 0; i < s.size(); i += 2 * k) {
            if (s.size() - i < k) {
                int left = i, right = s.size() - 1;
                while (left < right) {
                    char tmp = s[left];
                    s[left] = s[right];
                    s[right] = tmp;
                    left++;
                    right--;
                }
            }
            else if (s.size() - i >= k && s.size() - i < 2 * k) {
                int left = i, right = i + k - 1;
                while (left < right) {
                    char tmp = s[left];
                    s[left] = s[right];
                    s[right] = tmp;
                    left++;
                    right--;
                }
            }
            else {
                int left = i, right = i + k - 1;
                while (left < right) {
                    char tmp = s[left];
                    s[left] = s[right];
                    s[right] = tmp;
                    left++;
                    right--;
                }
            }
        }
        return s;
    }
};

Leetcode剑指offer05.替换空格

题目链接

题目思路:不能把字符串看作一个分裂的系统,注意字符串实质就是一个数组,我们要以数组的想法去处理它

双指针 延长字符串

s.resize(s.size() + count * 2);
class Solution {
public:
    string replaceSpace(string s) {
        int count = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == ' ') {
                count++;
            }
        }
        int length = s.size();
        s.resize(length + count * 2);
        int left = length - 1, right = s.size() - 1;
        while (left >= 0 && right >= 0 && left < right) {
            if (s[left] != ' ') {
                s[right] = s[left];
                right--;
                left--;
            }
            else {
                s[right--] = '0';
                s[right--] = '2';
                s[right--] = '%';
                left--; 
            }
        }
        return s;
    }
};

Leetcode151.反转字符串中的单词

题目链接

题目思路:分解问题

如何删除字符串中间的元素?

注意:数组中的元素只能覆盖,不能删除

reverse(s.begin(), s.end());
class Solution {
public:
    void deleteExtraBlank(string &s) {
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] == ' ' && s[i - 1] == ' ') {
                for (int j = i; j < s.size() - 1; ++j) {
                    s[j] = s[j + 1];
                }
                s.resize(s.size() - 1);
                i--;
            }
        }
    }
    string reverseWords(string s) {
        int len = s.size() - 1;
        while (s[len] == ' ') {
            len--;
        }
        s.resize(len + 1);
        reverse(s.begin(), s.end());
        len = s.size() - 1;
        while (s[len] == ' ') {
            len--;
        }
        s.resize(len + 1);
        deleteExtraBlank(s);
        int left = 0, right = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (i == s.size() - 1) {
                right = s.size() - 1;
                reverse(s.begin() + left, s.begin() + right + 1);
            }
            else if (s[i] == ' ') {
                right = i - 1;
                reverse(s.begin() + left, s.begin() + right + 1);
                left = i + 1;
            }
        }
        return s;
    }
};

Leetcode剑指offer58-II.左旋转字符串

题目链接

不如借助向量理解,反转字符串好比向量中的基底向量,我们可以借助基底表示其他的向量

class Solution {
public:
    string reverseLeftWords(string s, int n) {
        int len = s.size();
        reverse(s.begin(), s.end());
        reverse(s.begin(), s.begin() + len - n);
        reverse(s.begin() + len - n, s.end());
        return s;
    }
};

今日学习时长:1.5h (量子物理课)

你可能感兴趣的:(leetcode,c++)