dorseysen / One-Date-One-Question

this is a space for self-improvement of mine
1 stars 0 forks source link

2019-10-08:单词原位反转。难度 ★ #154

Open dorseysen opened 5 years ago

dorseysen commented 5 years ago

2019-10-08:单词原位反转 难度 ★ 'I am dorsey' => 'I ma yesrod'

dorseysen commented 5 years ago

// 2019-10-08:单词原位反转
// 难度 ★
// 'I am dorsey' => 'I ma yesrod'

// the first solution 较为直观但性能较差
const situReverse_0 = str => str.split(' ').map(item => item.split('').reverse().join('')).join(' ');

console.log(situReverse_0('I am dorsey'));

// the second solution 较为巧妙且性能更好
const situReverse = str => str.split(' ').reverse().join(' ').split('').reverse().join('');

return situReverse('I am dorsey');