版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27124771/article/details/84786559
题目描述: 编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
示例:
输入: "hello"
输出: "holle"
解题思路: 双指针典型题目,注意元音字母不要只考虑小写的。
public String reverseVowels(String s) {
if(s == null || s.length() == 0) return s;
char[] c = s.toCharArray();
int pre = 0, last = c.length - 1;
while(last > pre){
while(last > pre && !isVowel(c[last])) last--;
while(last > pre && !isVowel(c[pre])) pre++;
if(last > pre){
char t = c[last];
c[last] = c[pre];
c[pre] = t;
}
last--;
pre++;
}
return new String(c);
}
public boolean isVowel(char c){
c = Character.toLowerCase(c);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true;
return false;
}