JZ73 题解 | #翻转单词序列#
翻转单词序列
https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3
class Solution { public: vector<string> SplitStr(string& s, string op = " ") { vector<string> res; while (s.find(op) != string::npos) { auto found = s.find(op); res.push_back(s.substr(0, found)); s = s.substr(found + 1); } res.push_back(s); return res; } string ReverseSentence(string str) { std::vector<string> resVec = SplitStr(str); string strRes = ""; for (int i = resVec.size() - 1; i >= 0; i--) { strRes += resVec[i]; if (i != 0) { strRes += " "; } } return strRes; } };