自学内容网 自学内容网

【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;
    }
};

原文地址:https://blog.csdn.net/hlyd520/article/details/143983232

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!