-
Notifications
You must be signed in to change notification settings - Fork 0
/
345-反转字符串中的元音字母.java
44 lines (29 loc) · 932 Bytes
/
345-反转字符串中的元音字母.java
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
import java.util.ArrayList;
import java.util.HashMap;
class Solution {
public static String reverseVowels(String s) {
HashMap<Character, Integer> hm = new HashMap<>();
hm.put('a',1);
hm.put('e',1);
hm.put('i',1);
hm.put('o',1);
hm.put('u',1);
hm.put('A',1);
hm.put('E',1);
hm.put('I',1);
hm.put('O',1);
hm.put('U',1);
ArrayList <Integer> list = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (hm.containsKey(s.charAt(i))){
list.add(s.charAt(i)+0);
list.add(i);
}
}
StringBuilder sb = new StringBuilder(s);
for (int i = 0 , j = list.size() - 1; i < list.size() - 1 ; i += 2,j -= 2) {
sb.setCharAt(list.get(j), (char)(int)list.get(i));
}
return sb.toString();
}
}