Clean C++ two pointers solution


  • 0
     string reverseWords(string s) {
            string::iterator head=s.begin();
            string::iterator tail=s.begin();
            string::iterator next_head=head;
            while(tail<s.end()){
                while(*tail!=' '&&tail!=s.end()) tail++;
                next_head=tail+1;
                tail--;
                while(head<tail) swap(*head,*tail),head++,tail--;
                head=next_head;
                tail=head;
            }
            return s;
        }
    

Log in to reply
 

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