代码随想录算法训练营第八天| LeetCode344.反转字符串 541. 反转字符串II 剑指Offer 05.替换空格 151.翻转字符串里的单词剑指 Offer58-II.左旋转字符串

344.反转字符串

题目:344. 反转字符串

class Solution {
public:
    void reverseString(vector& s) {
        int l = 0, r = s.size() - 1;
        while(l < r){
            swap(s[l],s[r]);
            l++;
            r--;
        }
    }
};

541. 反转字符串II

题目:541. 反转字符串 II

class Solution {
public:
    string reverseStr(string s, int k) {
        int n = s.size();
        int y = 0;
        while(n >= 2* k){
            reverse(s.begin() + y,s.begin() + y + k);
            n = n - 2 * k;
            y += 2 * k ;
        }
        if(n < k) reverse(s.begin() + y, s.end());
        else if(n >= k && n < 2 * k) reverse(s.begin() + y, s.begin() + y + k);

        return s;
    }
};

//更精简的写法
class Solution {
public:
    string reverseStr(string s, int k) {
        int n = s.size(),pos = 0;
        while(pos < n){
            //剩余字符串大于等于k的情况
            if(pos + k < n) reverse(s.begin() + pos, s.begin() + pos + k);
            //剩余字符串不足k的情况 
            else reverse(s.begin() + pos,s.end());
            pos += 2 * k;
        }
        return s;
    }
};

剑指Offer 05.替换空格

题目:剑指 Offer 05. 替换空格

class Solution {
public:
    string replaceSpace(string s) {
        string ans = "";
        for(char c : s){
            if(c != ' ') ans += c;
            else{
                ans += "%20";
            }
        }
        return ans;
    }
};
//双指针法 不需要额外的新string
class Solution {
public:
    string replaceSpace(string s) {
        int count = 0;
        int n = s.size();
        for(char c : s){
            if(c == ' ') count++;
        }
        s.resize(n + count * 2);
        int m = s.size();
        for(int i = m - 1,j = n -1;  j >= 0; --i,--j){
            if(s[j] == ' ') {
                s[i] = '0';
                s[i-1] = '2';
                s[i-2] = '%';
                i -= 2;
            }else{
                s[i] = s[j];
            }

        }
        return s;
    }
};

151.翻转字符串里的单词

题目:151. 反转字符串中的单词

class Solution {
public:
    void removeSpaces(string& s){
        //快慢指针删除空格
        int slow = 0;
        for(int i = 0; i < s.size(); ++i){
            if(s[i] != ' ') {
                if(slow != 0) s[slow++] = ' ';
                while(i < s.size() && s[i] != ' '){
                    s[slow++] = s[i++];
                }

            }
        }
        s.resize(slow);
    }
    void reverse(string& s,int start,int e){
        //双指针reverse单词
        int l = start;
        int r = e;
        while(l < r){
            swap(s[l],s[r]);
            l++;
            r--;
        }
    }
    string reverseWords(string s) {
        removeSpaces(s);
        reverse(s,0,s.size()-1);
        int n = 0;
        for(int i = 0; i <= s.size(); ++i){
            if(s[i] == ' ' || i == s.size()){
                reverse(s,n, i - 1);
                n = i + 1;
            }
        }
        return s;
    }
};


    string reverseWords(string s) {
        removeSpaces(s);
        //借助额外空间
        vector v;
        string temp = "";
        for(char c : s){
            if(c != ' ') {
                temp += c;
            }else{
                v.push_back(temp);
                temp = "";
            }
        }
        v.push_back(temp);
        reverse(v.begin(),v.end());
        string ans = "";
        for(int i = 0 ; i < v.size(); ++i){
            ans += v[i];
            if(i != v.size() - 1)
            ans += " ";
        }
        return ans;
    }

剑指Offer58-II.左旋转字符串

题目:剑指 Offer 58 - II. 左旋转字符串

class Solution {
public:
    string reverseLeftWords(string s, int n) {
        string ans = "";
        for(int i = n;i < s.size(); i++){
            ans += s[i];
        }
        for(int i = 0; i < n; i++){
            ans += s[i];
        }
        return ans;
    }
};


//不开辟额外空间
class Solution {
public:
    string reverseLeftWords(string s, int n) {
        reverse(s.begin(),s.begin() + n);
        reverse(s.begin()+n,s.end());
        reverse(s.begin(),s.end());
        return s;
    }
};

总结

题型:字符串中元素的删除(快慢指针,类似于leetcode27),字符串的反转(双指针),反转前K个字节

技巧:想不开辟额外空间进行操作,需要想到双指针的方法。

你可能感兴趣的:(代码随想录算法训练营,算法,leetcode,字符串)