代码随想录算法训练营第八天| 344.反转字符串、541. 反转字符串II、卡码网:54.替换数字、151.翻转字符串里的单词、卡码网:55.右旋转字符串

代码随想录算法训练营第八天| 344.反转字符串、541. 反转字符串II、卡码网:54.替换数字、151.翻转字符串里的单词、卡码网:55.右旋转字符串

  • 344.反转字符串
  • 541. 反转字符串II
  • 卡码网:54.替换数字
  • 151.翻转字符串里的单词
  • 卡码网:55.右旋转字符串

344.反转字符串

题目链接
文章讲解

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

541. 反转字符串II

题目链接
文章讲解

class Solution {
public:
    void reverse(string &s, int start, int end) {
        for (int i = start, j = end - 1; i < j; i++, j--) {
            char tmp = s[i];
            s[i] = s[j];
            s[j] = tmp;
        }
    }

    string reverseStr(string s, int k) {
        int ptr = 0;
        int span = k << 1;
        int size = s.size();
        while (ptr < size) {
            int start = ptr;
            int end = ptr + k;
            if (end > size) end = size;
            reverse(s, start, end);
            ptr += span;
        }
        return s;
    }
};

卡码网:54.替换数字

题目链接
文章讲解

#include
using namespace std;

int main() {
    string s;
    cin >> s;
    for (char c : s) {
        if (c >= '0' && c <= '9')
            cout << "number";
        else
            cout << c;
    }
    return 0;
}

ACM模式特性:灵活解题

#include
using namespace std;

int main() {
    string s;
    cin >> s;
    int count = 0; // 数字字符个数
    for (char c : s) {
        if (c >= '0' && c <= '9') count++;
    }
    int oldSize = s.size();
    int newSize = oldSize + count * 5;
    s.resize(newSize);
    for (int i = newSize - 1, j = oldSize - 1; j >= 0; i--, j--) {
        if (s[j] > '9' || s[j] < '0') {
            s[i] = s[j];
        } else {
            s[i] = 'r';
            s[--i] = 'e';
            s[--i] = 'b';
            s[--i] = 'm';
            s[--i] = 'u';
            s[--i] = 'n';
        }
    }
    cout << s << '\n';
    return 0;
}

151.翻转字符串里的单词

题目链接
文章讲解

class Solution {
public:
    void reverse(string &s, int start, int end) {
        for (int i = start, j = end - 1; i < j; i++, j--) swap(s[i], s[j]);
    }

    void removeExtraSpaces(string &s) {
        int slow = 0, fast = 0;
        while (fast < s.size()) {
            if (s[fast] != ' ') {
                if (slow != 0) s[slow++] = ' ';
                while (fast < s.size() && s[fast] != ' ') s[slow++] = s[fast++];
            }
            fast++;
        }
        s.resize(slow);
    }

    string reverseWords(string s) {
        removeExtraSpaces(s);
        reverse(s, 0, s.size());
        for (int i = 0; i < s.size(); i++) {
            int start = i;
            while (i < s.size() && s[i] != ' ') i++;
            reverse(s, start, i);
        }
        return s;
    }
};

关键在于删除多余空白字符

卡码网:55.右旋转字符串

题目链接
文章讲解

#include
using namespace std;

void reverse(string &s, int start, int end) {
    for (int i = start, j = end; i < j; i++, j--) {
        char c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

int main() {
    int k;
    string s;
    cin >> k >> s;
    reverse(s, 0, s.size() - 1);
    reverse(s, 0, k - 1);
    reverse(s, k, s.size() - 1);
    cout << s << '\n';
    return 0;
}

408真题,简单

你可能感兴趣的:(算法)