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”.
Note:
The vowels does not include the letter “y”.
public String reverseVowels(String s) {
char[] ss = s.toCharArray();
int j = ss.length - 1, i = 0;
char temp;
while (i < j) {
while (i < j && !isVowel(ss[i]))
i++;
while (i < j && !isVowel(ss[j]))
j--;
temp = ss[i];
ss[i] = ss[j];
ss[j] = temp;
i++;
j--;
}
return String.valueOf(ss);
}
public boolean isVowel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return true;
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
return true;
return false;
}
时间: 2024-09-19 16:11:01