C++ Two Pointers Solution


  • 0
    E
    class Solution {
    public:
        string reverseWords(string s) {
            int first=0;int behind=0;
            int length = s.length();
            for(int i=0; i<length; ++i){
                if(s[i]==' '){
                    reverse(s.begin()+first,s.begin()+i);
                    first = i+1;
                }
            }
            reverse(s.begin()+first,s.end());
            return s;
        }
    };

Log in to reply
 

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