题解 | #翻转单词序列#

翻转单词序列

https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3

#include <algorithm>
class Solution {
public:
    void reverseword(string& s,int left,int right){
        char ch;
        while(left<right){
            ch=s[left];
            s[left]=s[right];
            s[right]=ch;
            left++;
            right--;
        }
    }
    string ReverseSentence(string str) {
        int n=str.length();
        reverseword(str,0, n-1);
        int fast=0;
        int slow=0;
        while(1){
            if(str[fast]==' '||str[fast]=='\0'){
                reverseword(str,slow,fast-1);
                if(fast==n) break;
                fast++;
                slow=fast;
            }else{
                fast++;
            }
        }
        return str;
    }
};

全部评论

相关推荐

10-10 01:10
已编辑
深圳大学 测试开发
面了100年面试不知...:六月到九月,四个项目一个实习,是魔丸吗
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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