4.7刷题记录(字符串专题)

发布于:2025-04-08 ⋅ 阅读:(17) ⋅ 点赞:(0)

题目列表 

344. 反转字符串 - 力扣(LeetCode)

541. 反转字符串 II - 力扣(LeetCode)

54. 替换数字(第八期模拟笔试)

151. 反转字符串中的单词 - 力扣(LeetCode)

 55. 右旋字符串(第八期模拟笔试)

28. 找出字符串中第一个匹配项的下标 - 力扣(LeetCode)

1.344. 反转字符串 - 力扣(LeetCode)

class Solution {
public:
    void reverseString(vector<char>& s) {
        int n=s.size();
        for(int i=0;i<=(n-1)/2;i++){
            char temp=s[i];
            s[i]=s[n-i-1];
            s[n-i-1]=temp;
        }
        return ;
    }
};

2.541. 反转字符串 II - 力扣(LeetCode)

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

reverse的使用是begin(),end()

3.54. 替换数字(第八期模拟笔试)

#include <iostream>
#include <vector>
using namespace std;

int main() {
    string s;
    cin >> s; // 使用 cin 接收输入
    vector<char> ans;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] >= '0' && s[i] <= '9') {
            ans.push_back('n');
            ans.push_back('u');
            ans.push_back('m');
            ans.push_back('b');
            ans.push_back('e');
            ans.push_back('r');
        } else {
            ans.push_back(s[i]); // 非数字字符直接存储
        }
    }
    for (char c : ans) { // 遍历输出容器中的元素
        cout << c;
    }
    cout << endl;
    return 0; // 主函数返回值应为 0 表示正常结束
}

4.151. 反转字符串中的单词 - 力扣(LeetCode)

class Solution {
public:
    string reverseWords(string s) {
        // 1.初始化
        string ans;
        int n = s.size();
        reverse(s.begin(), s.end());
        // 2.进行处理
        // 首先扫描每一个字符,如果可以构成单词,则将他翻转加入到ans中
        for (int i = 0; i < n;) {
            if (s[i] == ' ') {
                i++;
                continue;
            }
            int end = i;
            while (end < n && s[end] != ' ') { // 得到单词
                end++;
            }
            string sub = s.substr(i, end - i);
            reverse(sub.begin(), sub.end());
            if (!ans.empty()) {
                ans += " ";
            }
            ans += sub;
            i = end;
        }
        return ans;
    }
};

5.55. 右旋字符串(第八期模拟笔试)

#include <iostream>
using namespace std;

int main() {
    int k;
    cin >> k;
    string s;
    cin >> s;

    // 如果 k 大于字符串长度,取模处理
    k = k % s.size();

    // 循环右移字符串
    string ans = s.substr(s.size() - k) + s.substr(0, s.size() - k);

    cout << ans << endl;
    return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int k;
    cin >> k;
    string s;
    cin >> s;

    reverse(s.begin(),s.end());
    reverse(s.begin(),s.begin()+k);
    reverse(s.begin()+k,s.end());
    cout << s << endl;
    return 0;
}

reverse是左闭右开区间

6.28. 找出字符串中第一个匹配项的下标 - 力扣(LeetCode)

class Solution {
public:
    int strStr(string haystack, string needle) {
        if(haystack.size()<needle.size()){
            return -1;
        }
        if(needle.size()==0){
            return -1;
        }
        for(int i=0;i<=haystack.size()-needle.size();i++){
            int j=0;
            for(;j<needle.size();){
                if(needle[j]!=haystack[i+j]){
                    break;
                }
                else{
                    j++;
                }
            }
            if(j==needle.size()){
                return i;
            }
        }
        return -1;
    }
};

7.459. 重复的子字符串 - 力扣(LeetCode)

class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        //1.首先进行移动
        string sub=s+s;
        string doubled=sub.substr(1,sub.size()-2);
        return doubled.find(s)!=string::npos;
    }
};

特别巧妙的一道题,通过拼接发确定是否是循环字符串


网站公告

今日签到

点亮在社区的每一天
去签到