-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode345.java
More file actions
45 lines (41 loc) · 1.21 KB
/
LeetCode345.java
File metadata and controls
45 lines (41 loc) · 1.21 KB
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
import java.util.HashSet;
import java.util.List;
public class LeetCode345 {
public static void main(String[] args) {
// 输入:s = "hello"
// 输出:"holle"
System.out.println(new Solution345().reverseVowels("hello"));
// 输入:s = "leetcode"
// 输出:"leotcede"
System.out.println(new Solution345().reverseVowels("leetcode"));
}
}
class Solution345 {
public String reverseVowels(String s) {
char[] str = s.toCharArray();
HashSet<Character> set = new HashSet<>(List.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
int p1 = 0;
int p2 = s.length() - 1;
while (p1 < p2) {
while (p1 < p2 && !set.contains(str[p1])) {
p1++;
}
while (p2 > p1 && !set.contains(str[p2])) {
p2--;
}
if (p1 >= p2) {
break;
}
// System.out.println(p1 + " " + p2);
swap(str, p1, p2);
p1++;
p2--;
}
return new String(str);
}
public void swap(char[] str, int p1, int p2) {
char c = str[p1];
str[p1] = str[p2];
str[p2] = c;
}
}