C++ easy way


  • 0
    A

    class Solution {
    public:
    string reverseWords(string s) {
    int i,j=0,n,s1=0,p=0;
    n=s.length();
    for(i=0;i<=n;j++)
    {
    if(s[j]==0)
    {
    while(i<=--j)
    swap(s[i++],s[j]);
    return s;
    }
    if(s[j]==' ')
    {
    s1=j-- +1;
    while(i<=j)
    swap(s[i++],s[j--]);
    i=j=s1;
    }

     }
        return s;
    

    }

    };


Log in to reply
 

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