Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fireairforce opened this issue Feb 26, 2020 · 0 comments
Open

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

fireairforce opened this issue Feb 26, 2020 · 0 comments
Labels
easy This issue or pull request already exists 剑指offer This doesn't seem right

Comments

@fireairforce
Copy link
Owner

水题,记得处理空格:

/**
 * @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 fireairforce added easy This issue or pull request already exists 剑指offer This doesn't seem right labels Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy This issue or pull request already exists 剑指offer This doesn't seem right
Projects
None yet
Development

No branches or pull requests

1 participant