A solution using stringstream in cpp


  • 0
    F
    class Solution {
    public:
    	void reverseWords(string &s) 
    	{
    		stringstream scin(s);
    		string word, res;
    		while (scin >> word)
    		{
    			if (res.length() != 0)
    				word += " ";
    			res = word + res;
    		}
    		s = res;
    	}
    };

  • 0
    S

    Thanks for your post. However it would be better to share solution with correct code format and elaborated thoughts. Please read the Discuss FAQ for more info. Take a look at good sharing example


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.