I am going to lose my job. Day 1 Sword Finger Offer 5. Replace spaces

Affected by the epidemic, it can only be a home. The atmosphere without a laboratory has completely become a salted fish, and the small papers have not been restored to the teacher. Today, I began to record the process of brushing the sword and finger offer.
Meow, this question is almost 2 hours old. This was a very simple question. The string on the button has space restrictions. It will report an error when it crosses the boundary. Finally, it was solved with a resize. First
look at the topic:
please implement a function to replace each space in the string s with "% 20".

Example 1:
Input: s = "We are happy."
Output: "We% 20are% 20happy."

The idea used in this question is to rewrite the string from back to front. Here we need to first count the length of the string and the number of spaces. It can be solved with a while loop. The end of the string is the '\ 0' symbol, see it Stop counting, you can also use a for loop to solve, anyway, you can use s.size () and s.length to get the length of the string. After calculating how much space is needed after rewriting, after rewriting = before rewriting + 2 * number of spaces, this question needs to be understood.
Then declare two pointers, Pointer 1 points to the last bit before rewriting, Pointer 2 points to the last bit after rewriting, under normal circumstances, the two pointers are shifted to the left by 1 space every cycle, and the value of pointer 1 is assigned to the pointer 2. After pointer 1 points to a space, pointer 1 moves to the left by 1 click, then writes '0', '2', and '%' to pointer 2 and moves to the left 3 times, even when the two pointers have the same index finished.
code show as below:

class Solution {
public:
    string replaceSpace(string s) {
        
        int num_space = 0;
        int length_1 = 0;
        
        int i = 0;
        while(s[i] != '\0')
        {
            if (s[i] == ' ')
            num_space++;
            length_1++;
            i++;
        }
      
        cout<<num_space<<endl;
        cout<<length_1<<endl;
        int length_2 = length_1 + 2*num_space;
        cout<<length_2<<endl;
        s.resize(length_2);
        while(length_1>=0 &&length_2>length_1)
        {
            if (s[length_1] == ' ')
            {
                cout<<'a'<<length_2<<'\t'<<length_1<<endl;
                
                s[length_2] = '0';
                cout<<s[length_1]<<'\t'<<s[length_2]<<endl;
                length_2--;
                s[length_2] = '2';
                cout<<s[length_1]<<'\t'<<s[length_2]<<endl;
                length_2--;
                s[length_2] = '%';
                cout<<s[length_1]<<'\t'<<s[length_2]<<endl;
                length_2--;
                
                length_1--;
                
            }
            else 
            {
                cout<<'b'<<length_2<<'\t'<<length_1<<endl;
                
                s[length_2] = s[length_1];
                length_2--;
                length_1--;
                cout<<s[length_1]<<'\t'<<s[length_2]<<endl;
                
                
            }
            
        }
        return s;
    }
    
};

It should be noted that the
double pointer method is very classic in buckle, you must learn to use it; the
string ends with '\ 0'; s.size () and s.length () can calculate the length.
If you don't want to cross the boundary, use resize The function expands the boundary. I thought of declaring another string before, but it required me to declare the size, which is annoying! I wasted a lot of time.
Brushing questions is the process of accumulating problem-solving experience, Olly gives!
In the evening, fight for another one.

Published 3 original articles · Likes0 · Visits15

Guess you like

Origin blog.csdn.net/qq_41227231/article/details/105664624