剑指OFFER----面试题58 - I. 翻转单词顺序

链接:https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/

代码

class Solution {
public:
    string reverseWords(string s) {
        if (s.empty()) return s;
        int len = 0;
        string res = "";
        for (int i = s.size() - 1; i >= 0; --i) {
            if (s[i] == ' ' && len != 0) {
                res += s.substr(i + 1, len) + " ";
                len = 0;
                continue;
            } 
            if (s[i] != ' ') len++;
        }
        if (len != 0) res += s.substr(0, len) + " ";
        if (res.size() > 0) res.erase(res.size() - 1, 1);
        return res;
    }
};
上一篇:ASP.NET 5探险(1):Azure中配置连接字符串、独立项目执行EF7数据迁移


下一篇:“58同城”架构师分享:联盟广告平台架构及实践