
Given a string s, reverse only all the vowels in the string and return it.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
320Please respect copyright.PENANA1IP4JxqO69
Two Pointers
class Solution {320Please respect copyright.PENANAOyhqyewUkF
// Return true if the character is a vowel (case-insensitive)320Please respect copyright.PENANAD8072oVyFB
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU320Please respect copyright.PENANA2EUKdKHO5v
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。320Please respect copyright.PENANAk1HnhEEViV
boolean isVowel(char c) {320Please respect copyright.PENANA3U1n44l2ry
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'320Please respect copyright.PENANAwmZGZ3HlOl
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';320Please respect copyright.PENANAgmFHsZDIpS
}320Please respect copyright.PENANAxIk9UYue65
320Please respect copyright.PENANAewDA3fKk3u
// Function to swap characters at index x and y320Please respect copyright.PENANAXwjzG15Nq5
void swap(char[] chars, int x, int y) {320Please respect copyright.PENANA9mZ8AtcWTg
char temp = chars[x];320Please respect copyright.PENANAw1iE86IDTL
chars[x] = chars[y];320Please respect copyright.PENANAvGLbw9dCxm
chars[y] = temp;320Please respect copyright.PENANATwrrEsanWL
}320Please respect copyright.PENANA85B2ACUxMf
320Please respect copyright.PENANAfZZuF9K3MK
public String reverseVowels(String s) {320Please respect copyright.PENANAguQRSt12tI
// 設定最左的字母是[0]320Please respect copyright.PENANAP5iJF16fhJ
int start = 0;320Please respect copyright.PENANAGg4gFXGwWR
// 設定最右的字母是[文字總長度-1].320Please respect copyright.PENANAAzldArUI4L
int end = s.length() - 1;320Please respect copyright.PENANA4MQIxBedW2
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的320Please respect copyright.PENANAF80R6pZsbx
char[] sChar = s.toCharArray();320Please respect copyright.PENANAa09bKuaPtR
320Please respect copyright.PENANA4RwM9zsmq5
// While we still have characters to traverse320Please respect copyright.PENANAa3dextEKmd
// while the word more than one letter, do this function320Please respect copyright.PENANA4B435t9kme
while (start < end) {320Please respect copyright.PENANApiO9mh9k4Y
// Find the leftmost vowel320Please respect copyright.PENANAh2YLz1vlQv
// while start 少於 string length() 同時 [start] 不是vowel,return start ++320Please respect copyright.PENANAsJGnmnx9J5
while (start < s.length () && !isVowel(sChar[start])) {320Please respect copyright.PENANAjuOESrDY75
start++;320Please respect copyright.PENANAKjEdv9Pnd7
}320Please respect copyright.PENANAWdXrhyJk5B
// Find the rightmost vowel320Please respect copyright.PENANAx9DufHLYHu
// while end 大於 0 同時 [end] 不是vowel,return end --320Please respect copyright.PENANACOxFyHNfAl
while (end >= 0 && !isVowel(sChar[end])) {320Please respect copyright.PENANAJPfSX545ML
end--;320Please respect copyright.PENANA17S1ife0Fs
}320Please respect copyright.PENANAGbRGWYw8MO
// Swap them if start is left of end320Please respect copyright.PENANAK3sp55STbQ
// swap function: (in what string, value 1, value 2), swap value 1 and 2320Please respect copyright.PENANAYxQUlzGGdN
if (start < end) {320Please respect copyright.PENANAnLrpn53qZ3
swap(sChar, start++, end--);320Please respect copyright.PENANAGT2mXDqfF3
}320Please respect copyright.PENANANp5IWULfG4
}320Please respect copyright.PENANAxRkruOCUT9
320Please respect copyright.PENANA86syvmv4Gq
// Converting char array back to String320Please respect copyright.PENANAmAoFDpTFuC
// 顯示新的String320Please respect copyright.PENANAz7PdZg9uDR
return new String(sChar);320Please respect copyright.PENANATqmIKWeybT
}320Please respect copyright.PENANACQtMju1Koz
};