题解 | #翻转单词序列#
翻转单词序列
https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3
/** *string 中的find 和 substr方法,都是严格按照左闭右开原则 * **/ #include <algorithm> class Solution { public: stack<string>st; string ReverseSentence(string str) { // reverse(str.begin(), str.end()); string substr ; string head_str; string ans_str ; while (str.find(" ") != string::npos) { head_str = str.substr(0, str.find(" ")); st.push(" "+head_str); str = str.substr(str.find(" ")+1); } st.push(str); while (!st.empty()) { string tmpstr = st.top(); for (int i=0; i<tmpstr.size(); i++) { ans_str.push_back(tmpstr[i]); } st.pop(); } return ans_str; } };