# 345. Reverse Vowels of a String ## 題目概要 找出字串中的母音後將找到的母音順序反轉,再依次放回本來母音的位置。 ``` Example 1: Input: s = "hello" Output: "holle" Example 2: Input: s = "leetcode" Output: "leotcede" ``` ## 解題技巧 - 母音大小寫都要考慮到。 - 將字串每個字符拆分開存成 array,遍歷尋找母音,並將母音的索引和值分開存成兩個陣列。存值的陣列需要反轉,反轉後將反轉後的母音順序依次放回字串中母音索引值的位置。 ## 程式碼 ```javascript= /** * @param {string} s * @return {string} */ var reverseVowels = function(s) { let str = s.split(''); let vowels = ['a', 'A', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u']; let result = []; let index = []; for(let i = 0; i < s.length; i++) { if(vowels.includes(s[i])) { result.push(s[i]); index.push(i); } } result.reverse(); for (let i = 0 ; i < index.length; i++) { str[index[i]] = result[i]; } return str.join(''); }; ``` 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up