-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06.js
More file actions
34 lines (26 loc) · 752 Bytes
/
Copy path06.js
File metadata and controls
34 lines (26 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 345. 反转字符串中的元音字母 难度:简单
// 思路 先找出元音字母位置和所有元音字母,插入
/**
* @param {string} s
* @return {string}
*/
var reverseVowels = function(s) {
if(s.length<2) return s; // 判断 输入s=""或者s=" " 这种情况
var arr = s.split('');
var yuan = [];
var newarr = arr.map( function(x){
if(/[aeiouAEIOU]/.test(x)){ // 匹配到元音字母
yuan.push(x);
return '-1';
}else{
return x;
}
});
for(var i in newarr){
if(newarr[i] == '-1'){
newarr[i] = yuan.pop(); // 插入
}
}
return newarr.join('');
};
console.log(reverseVowels('leecode'))