Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
只交换元音字母。思想类似于。。。。切分!
头尾分别开始,扫描,遇到元音字母为止,交换,两个指针相遇了就处理完毕。
public String reverseVowels(String s)
{
int len=s.length();
char[] carr=s.toCharArray();
int i=0;
int j=len-1;
while(i<j)
{
while(i<len&&!isvowel(s.charAt(i)))
i++;
while(j>=0&&!isvowel(s.charAt(j)))
j--;
if(i<j)
{
char temp=carr[i];
carr[i]=carr[j];
carr[j]=temp;
i++;
j--;
}
}
return new String(carr);
}
public boolean isvowel(char c)
{
switch (c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
return true;
default:
return false;
}
}