问题描述:编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例 1:
输入: "hello"
输出: "holle"
示例 2:
输入: "leetcode"
输出: "leotcede"
class Solution {
public String reverseVowels(String s) {
if(s.length()<=1){
return s;
}
char[] chs = s.toCharArray();
int i =0;
int j =chs.length-1;
while(i<j){
if(isVowels(chs[i]) && isVowels(chs[j])){
char tmp = chs[i];
chs[i] = chs[j];
chs[j] = tmp;
i++;
j--;
}else if(isVowels(chs[i]) && !isVowels(chs[j])){
j--;
}else if(!isVowels(chs[i]) && isVowels(chs[j])){
i++;
}else{
i++;
j--;
}
}
return new String(chs,0,chs.length);
}
//判断该字符是否是元音字母
private boolean isVowels(char c){
if((c=='a'|| c=='e'|| c=='i'||c=='o'||c=='u' ) ||
(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')){
return true;
}
return false;
}
}
参考:
标签:String,示例,反转,字母,字符串,元音 From: https://www.cnblogs.com/i9code/p/18005400