My simple c++ solution


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

    };


Log in to reply
 

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