String exercise【2】

The study arrangement is based on leetcode 151~

I wrote about the topic of intercepting strings in Huawei's computer test before, so I thought about using this method, but there are steps to deal with before using this method:

Similar to the question of "coordinate movement", its string form: A20; S30; use the substr() function to intercept the string

That is, the first character is not ";", and the last one is ";"

The first character ensures that there will be no ";" in the starting position of the string after interception during interception, and the last one is ";", which ensures that the intercepted string can intercept the last string of characters.

The spaces in this question are random, that is, there may be multiple spaces between the two strings. When the head character is a space , the head character still retains a space when intercepting ; when the tail character is not a space , the last string of strings cannot be intercepted.

Therefore, we need to "remove extra spaces" from the string first;

Use the erase() function that comes with string to delete. This method is easy to think of, but be careful to avoid pitfalls!

When traversing a string, if the traversal and deletion are performed in the same direction , there will still be spaces that cannot be deleted! ! ! Ultimately this is because the string itself changes dynamically based on your delete operations, i.e. the index of the word changes as the traversal and delete go in the same direction!

If there is " s space space space space sss ", delete the first space (index 1), the index of the second space becomes 1 at this time, and then i++, i=2 is the index of the third space, judge at this time condition if yes

string[i]==string[i+1],erase(i,1)

This is to pass the judgment smoothly, and the next space will not be deleted.

If the judgment condition is

string[i]==string[i-1],erase(i,1)

When the second space is traversed, the current space will be deleted, and the next space is i, but after i=i+1 after the loop, the space cannot be deleted.

Similarly, if the delete instruction is erase(i+1,1) or erase(i-1,1), it will also cause problems! ! !

Therefore, the traversal and deletion operations cannot be performed in the same direction: choose reverse traversal forward deletion, and delete the spaces that have been traversed, thus avoiding this problem.

One last thing to note is that to remove all extra spaces, you can add a space at the end of the string to ensure that the last string can be successfully intercepted when using substr().

class Solution {
public:
    string reverseWords(string s)
{
    int len = 0;
    int num = 0;
    vector<string>ss;
    string sss;
  
        for (int i = s.size() - 1; i > 0; i--) 
        {
            if (s[i] == s[i - 1] && s[i] == ' ') 
            {
                s.erase(s.begin() + i);
            }
        }
        // 删除字符串最后面的空格
        if (s.size() > 0 && s[s.size() - 1] == ' ') {
            s.erase(s.begin() + s.size() - 1);
        }
        // 删除字符串最前面的空格
        if (s.size() > 0 && s[0] == ' ') {
            s.erase(s.begin());
        }
    if (s[s.length() - 1] != ' ')
    {
        s += ' ';
    }
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] != ' ')
        {
            ++len;
            continue;
        }
        ss.push_back(s.substr(i - len, len));
        len = 0;
    }
    for (int i = ss.size() - 1; i >= 0; i--)
    {
        sss += ss[i];
        if (i > 0)
        {
            sss += ' ';
        }
    }
    return sss;
    }
};

Although it is not a good code, this idea has also allowed me to exercise the ability to intercept strings again, which is a good thing. The official answer will take a closer look, and some library functions have to be supplemented~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324200580&siteId=291194637