C++ Solution


  • 0
    B
    class Solution {
    public:
        string reverseWords(string s) {
            int pos1=0,pos2=0,i=0;
            while ((pos2=s.find_first_of(' ',pos1))!=string::npos){
                reverse(s.begin() + pos1, s.begin() + pos2);
                pos1=pos2+1;
            }
            pos2=s.length();
            reverse(s.begin() + pos1, s.begin() + pos2);
            return s;
        }
    };
    

Log in to reply
 

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