leetCode-Record icon indicating copy to clipboard operation
leetCode-Record copied to clipboard

面试题58 - I. 翻转单词顺序

Open fireairforce opened this issue 5 years ago • 0 comments

水题,记得处理空格:

/**
 * @param {string} s
 * @return {string}
 */
var reverseWords = function(s) {
   s =  s.split(' ').reverse().join(' ').trim().split('');
   for(let i = 0;i<s.length;i++) {
       if(s[i] === ' ') {
           while(i + 1 <= s.length - 1 && s[i + 1] === ' ') {
               s[i+1] = ''
               i++;
           }
       }
   }
   return s.join('')
};

fireairforce avatar Feb 26 '20 16:02 fireairforce