-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path345.反转字符串中的元音字母.js
58 lines (58 loc) · 1.07 KB
/
345.反转字符串中的元音字母.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* @lc app=leetcode.cn id=345 lang=javascript
*
* [345] 反转字符串中的元音字母
*
* https://leetcode-cn.com/problems/reverse-vowels-of-a-string/description/
*
* algorithms
* Easy (47.74%)
* Likes: 55
* Dislikes: 0
* Total Accepted: 13.2K
* Total Submissions: 27.6K
* Testcase Example: '"hello"'
*
* 编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
*
* 示例 1:
*
* 输入: "hello"
* 输出: "holle"
*
*
* 示例 2:
*
* 输入: "leetcode"
* 输出: "leotcede"
*
* 说明:
* 元音字母不包含字母"y"。
*
*/
/**
* @param {string} s
* @return {string}
*/
var reverseVowels = function(s) {
const arr = s.split("");
const vowels = ["a", "e", "o", "i", "u", "A", "E", "O", "I", "U"];
let lp = 0,
rp = s.length - 1;
while (lp < rp) {
if (!~vowels.indexOf(arr[lp])) {
lp++;
continue;
}
if (!~vowels.indexOf(arr[rp])) {
rp--;
continue;
}
const tmp = arr[lp];
arr[lp] = arr[rp];
arr[rp] = tmp;
lp++;
rp--;
}
return arr.join("");
};