[LeetCode]Reverse Words in a String

class Solution {
public:
    void reverseWords(string &s) {
        string word;
    	stack<string> wordsStack;
    	stringstream ss(s);
    	while(ss>>word)
    	{
    		wordsStack.push(word);
    	}
    	string res = "";
    	bool first = true;
    	while(!wordsStack.empty())
    	{
    		if(!first)
    			res += " ";
    		res += 	wordsStack.top();
    		wordsStack.pop();
    		first = false;
    	}
    	s = res;
    }
};

你可能感兴趣的:([LeetCode]Reverse Words in a String)