链接: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;
}
};