My simple Javascript solution


  • 0
    K
    var reverseWords = function(str) {
    str=str.trim();
    
    if(str.length==0 || !str)return "";
    var s=str.split(" ").reverse();
    var r="";
    for(var i=0;i<s.length;i++){
        if(s[i].localeCompare("")!=0)r=r+s[i]+" ";
    }
    r=r.trim();
    //console.log(r);
    
    return r;
    

    };


Log in to reply
 

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