Cpp solution in 9 lines


  • 1
    T
    void reverseWords(string& s) {
    	reverse(s.begin(), s.end());
    	stringstream ss(s);
    	s.clear();
    	string tmp;
    	while(ss >> tmp) {
    		reverse(tmp.begin(), tmp.end());
    		s += tmp + " ";
    	}
    	if(!s.empty()) s.erase(s.size() - 1);
    }
    

    I guess stringstream is useful at anytime you need to separate a string.


Log in to reply
 

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