Code Caprice Algorithm Training Camp 15th Day 8 | 344. Reverse String, 541. Reverse String II, Sword Pointer Offer 05. Replace Space, 151. Flip Words in String, Sword Pointer Offer58-II. Left Rotation

344. Reverse a String

Suggestion: This question is a basic question about strings. It is to investigate the realization of the reverse function. At the same time, it is also clear when to use the library function and when not to use the library function --- the library function can only be used when it is part of the problem.

Topic Link: Lituo

Idea: This question uses a swap function.

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

541. Reverse a String II

Suggestion: This question is advanced again. First go to do it independently, and then read the solution, and you will have a deep understanding of the coding skills.

Topic Link: Lituo

Idea: Each processing is processed by 2k, flipping k each time, if it is not enough for 2k, flip all. 2k is traversed for the loop. The function used here is the reverse function. Note that when the reverse(i, i+k) function is reversed, it is left closed and right opened.

There is one place in the following code that needs attention, that is, during the conversion process, there is a s.begin() + a certain number. When I was writing the code, I forgot s.begin() and the code always Can't compile.

class Solution {
public:
    string reverseStr(string s, int k) {

        //首先是以2k为循环,依次进行下去
       for(int i = 0;i<s.size();i+=(2*k))
       {
           if(i+k<=s.size())
           {
               reverse(s.begin()+i,s.begin()+i+k);
               continue;//跳出循环
           }
            //最后剩下的那一点
           reverse(s.begin()+i,s.begin()+s.size());
       }
       return s;


    }
};

Sword Pointer Offer 05. Replace spaces 

Suggestion: For linear data structures, filling or deleting, post-order processing will be much more efficient. Have a good experience.

Topic Link: Lituo

class Solution {
public:
    string replaceSpace(string s) {

        //首先记录空格的数量
        int count_space = 0;

        for(int i = 0;i < s.size();i++)
        {
            if(s[i]==' ')
            {
                count_space++;
            }
        }

        int oldsize  = s.size();

        s.resize(s.size()+count_space*2);

        int newsize = s.size();

        //开始从后往前进行遍历
        for(int i = newsize-1,j=oldsize-1;j<i;i--,j--)
        {
            if(s[j]!=' ')//注意这个条件
            {
                s[i]=s[j];
            }
            else{
            s[i] = '0';
            s[i-1] = '2';
            s[i-2] = '%';

            i-=2;
            }
           

        }
        return s;





    }
};

151. Flip words in a string 

Suggestion: This question basically covers all the string operations just done, but even if you know the idea of ​​solving the problem, the code for this question is not easy to write, so you need to practice more. 

Idea: __wo_he__ -> wo_he -> ow_eh -> wo_he.
Do not use erase, because the time complexity of erase is O(n), and the time complexity has increased. For example: if () {erase once} So here use double pointer method to solve.

Define a fast pointer and define a slow pointer.

class Solution {
public:

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

        //这道题比较难,看视频没有看明白,按照自己的思路来
        //首先是移除空格,所有的字母之间只是存在一个空格并且首尾字母之间不存在空格
        void removeBlank(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);

        }

    string reverseWords(string s) {
        removeBlank(s);//去除空格

        reverse(s,0,s.size()-1);//翻转一下
        int start = 0;

        for(int i = 0;i<s.size();i++)
        {
            if((i != (s.size()-1)) && (s[i]==' '))//注意条件,到达尾端
            {
                reverse(s,start,i-1);
                start = i+1;//这句代码写错了,导致我浪费了很多时间
            }
            else if(i == (s.size()-1))
            {

                reverse(s,start,i);
            }
            
        }
        
        return s;
    }
};

Sword Pointing to Offer58-II. Left Rotate String 

Suggestion: If you have not touched the solution in the problem solution, you should not think of it

Topic Link: Lituo

The idea of ​​this question is very simple, as shown in the following figure:

The first is to reverse the first n strings, then reverse the last n strings, and then perform the overall flip.

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;

    }
};

 




 

Guess you like

Origin blog.csdn.net/m0_47489229/article/details/130978601