345.反转字符串中的元音字母
1
class Solution {
2
public String reverseVowels(String s) {
3
char[] array = s.toCharArray();
4
int i = 0;
5
int j = array.length - 1;
6
while(i < j){
7
//不是偶数i++ j--
8
if(!isVowel(array[i])){
9
i++;
10
}
11
if(!isVowel(array[j])){
12
j--;
13
}
14
//如果都是偶数进行交换
15
if(isVowel(array[i]) && isVowel(array[j])){
16
char c = array[i];
17
array[i] = array[j];
18
array[j]= c;
19
i++;
20
j--;
21
}
22
}
23
24
return new String(array);
25
26
}
27
private boolean isVowel(char c){
28
return c =='a' || c =='e' || c =='i' || c =='o'|| c =='u' ||
29
c =='A' || c =='E' || c =='I' || c =='O'|| c =='U';
30
}
31
}
Copied!
最近更新 4mo ago
复制链接