快特么气成傻逼了才AC
#include <iostream> #include <stack> using namespace std; stack<string> st; class Solution { public: void reverseWords(string &s) { int i = s.find_first_not_of(" "); if(i > s.size()) { s = ""; return; } string t; while(i < s.size()) { if(s[i] != ' ') { t += s[i]; } else { if(!t.empty()) { st.push(t); t = ""; } } i++; } if(!t.empty()) { st.push(t); t = ""; } string re; while(!st.empty()) { re += st.top(); st.pop(); re += " "; } re.erase(re.find_last_not_of(" ")+1); s = re; } }; int main(void) { //string a = "the sky is blue"; string a = "a"; Solution s; s.reverseWords(a); cout << a << endl; return 0; }