Bootstrap

【C++习题】10.反转字符串中的单词 lll

题目:

链接🔗:557.反转字符串中的单词 lll

题目:

163713fea159122c6ab40a2a3439b7f9


代码:

class Solution {
public:
    void Reverse(string &s, int start, int end)
    {
        char tmp;
        while(start < end)
        {
            tmp = s[start];
            s[start] = s[end];
            s[end] = tmp;

            start++;
            end--;
        }
    }

    string reverseWords(string s) 
    {
        size_t start = 0;
        size_t end = 0;
        while(start < s.size())
        {
            end = s.find(' ', start);
            if(end == string::npos)
            {
                end = s.size();
                break;
            }
            Reverse(s, start, end-1);
            start = end+1;
        }
        Reverse(s, start, end-1);
        return s;
    }
};
;