[javascript] 129ms any suggestion is welcome


  • 0
    C

    /**

    • @param {string} s

    • @return {string}
      */
      var reverseWords = function(s) {
      var arr1 = [];
      var ans = [];
      var ans2 = ""

      //reverse arr1
      arr1 = s.split(" ");
      for (let i = 0 ;i<arr1.length;i++){
      var arr3 = "";
      arr3 = arr1[i].toString();
      arr3 = arr3.split("").reverse().join("");

       ans.push(arr3);
      

      }

      var add = ans.pop();

      for(let i=0; i<ans.length; i++){
      ans2 += ans[i]+" ";
      }
      return(ans2+add);
      // if there are any "," in string ,then they would be replace so do not use replace
      //return(ans.toString().replace(/,/g," "))
      };


Log in to reply
 

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