JavaScript Solution


  • 0

    I used Array,This method will waste space,nevertheless I think this solution is easily understanding

    /**
     * @param {string} s
     * @return {string}
     */
    var reverseWords = function(s) {
        let arr = [];
        let word = '';
        for(let i=s.length-1;i>=0;i--){
            if(s[i]===' '){
                arr.unshift(word);
                word = '';
                continue;
            }
            word += s[i];
        }
        arr.unshift(word);
        return arr.join(' ');
    };
    

Log in to reply
 

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