编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: "hello"
输出: "holle"
示例 2:
输入: "leetcode"
输出: "leotcede"
说明:
元音字母不包含字母"y"
1/** 2 * @param {string} s 3 * @return {string} 4 */ 5function judgeVowel(c) { 6 return c == ‘a‘ || c == ‘e‘ || c == ‘i‘ || c == ‘o‘ || c == ‘u‘ || 7 c == ‘A‘ || c == ‘E‘ || c == ‘I‘ || c == ‘O‘ || c == ‘U‘ 8} 910var reverseVowels = function (s) {11 var j = s.length - 112 var a = s.split(‘‘)13 for (var i = 0; i < j;) {14 if (!judgeVowel(s[i])) {15 i++16 } else {17 if (!judgeVowel(s[j])) j--18 else {19 var temp = s[i]20 a[i] = s[j]21 a[j] = temp22 i++23 j--24 }25 }26 }27 return a.join(‘‘)28}
原文地址:https://www.cnblogs.com/rencoo/p/9783732.html
时间: 2024-11-08 17:14:05