C++ solution, easy to understand


  • 0
    M
    class Solution {
    public:
        void reverseWords(string &s) {
            reverse(s.begin(),s.end());
            int sign = 0;
            for(int i=0;i<s.size();i++){
                if(s[i] != ' ')
                    continue;
                else{
                    reverse(s.begin()+sign, s.begin()+i);
                    sign = i+1;
                }
            }
            reverse(s.begin()+sign, s.end());
        }
    };

Log in to reply
 

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