Simple accepted C++ Code


  • 0
    M
    class Solution {
    public:
        void reverseWords(string &s) {
           
            string res = "";
            if(s.length()==0) return;
            int i=s.length()-1,end=i, start=i;
            while(i>=0){
                if(s[i--]!=' ') {
                    start=i+1;
                    if((i>=0 && s[i]==' ')||i<0) {
                        res+=s.substr(start,end-start+1)+" ";
                        end = start-1;
                    }
                }
                else end--;
            }
            s=res.substr(0,res.length()-1);
        }
    };

Log in to reply
 

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