Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b207ed

Browse files
String: 345. Reverse Vowels of a String
1 parent 58ce42b commit 1b207ed

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://leetcode.com/problems/reverse-vowels-of-a-string/description/
2+
/**
3+
* 345. Reverse Vowels of a String
4+
* @param {string} s
5+
* @return {string}
6+
*/
7+
const set = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
8+
var reverseVowels = function(s) {
9+
let left = 0;
10+
let right = s.length - 1;
11+
const arr = s.split('');
12+
let tem = ''
13+
while (left < right) {
14+
if (set.has(arr[left])) {
15+
if (set.has(arr[right])) {
16+
tem = arr[right];
17+
arr[right] = arr[left];
18+
arr[left] = tem;
19+
left++;
20+
right--;
21+
} else {
22+
right--
23+
}
24+
} else if (set.has(arr[right])) {
25+
left++;
26+
} else {
27+
left++;
28+
right--;
29+
}
30+
}
31+
return arr.join('');
32+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /