题解 | #倒置字符串#C++解法,通过reverse逆置

倒置字符串

https://www.nowcoder.com/practice/ee5de2e7c45a46a090c1ced2fdc62355

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() 
{
    string s;
    getline(cin, s);
    reverse(s.begin(), s.end());

    auto start = s.begin();
    while(start != s.end())
    {
        auto end = start;
        while(end != s.end() && *end != ' ')
        {
            end++;
        }
        reverse(start, end);

        if(end != s.end())
        {
            start = end + 1;
        }
        else 
        {
            start = end;
        }
    }
    cout << s << endl;
    return 0;
}

全部评论

相关推荐

飞屋一号:实话实说就行,先争取一下能不能线上,不行就直接放弃,付出与回报不成正比
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务