
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.
298Please respect copyright.PENANAjmvqOuSvTh
Two Pointers
class Solution {298Please respect copyright.PENANAYGLci2zVLH
// Return true if the character is a vowel (case-insensitive)298Please respect copyright.PENANAQ0tziIjegL
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU298Please respect copyright.PENANAaqTLFhC2XX
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。298Please respect copyright.PENANAcKL5MlifYn
boolean isVowel(char c) {298Please respect copyright.PENANA5rIhohmlQh
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'298Please respect copyright.PENANA74v6fnBkwv
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';298Please respect copyright.PENANAzTYMHoAjfK
}298Please respect copyright.PENANA6n9jePdmka
298Please respect copyright.PENANAVKugglKCzW
// Function to swap characters at index x and y298Please respect copyright.PENANAJ2H3B8PiM5
void swap(char[] chars, int x, int y) {298Please respect copyright.PENANABr7ocWVeRs
char temp = chars[x];298Please respect copyright.PENANAfDtwtZBhZM
chars[x] = chars[y];298Please respect copyright.PENANAbgLvmXuxEg
chars[y] = temp;298Please respect copyright.PENANAEFVkN63J5h
}298Please respect copyright.PENANAcmWphe1YwY
298Please respect copyright.PENANAJy2PPeW4NT
public String reverseVowels(String s) {298Please respect copyright.PENANAA6yz1RLpEY
// 設定最左的字母是[0]298Please respect copyright.PENANAa5Ne86OA6n
int start = 0;298Please respect copyright.PENANASKRbVwzcAe
// 設定最右的字母是[文字總長度-1].298Please respect copyright.PENANATiHoXWDQWJ
int end = s.length() - 1;298Please respect copyright.PENANArJrOjEdXhx
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的298Please respect copyright.PENANA5UK7BHgcPp
char[] sChar = s.toCharArray();298Please respect copyright.PENANA2RwEuYcTlM
298Please respect copyright.PENANAd2WASOhHvu
// While we still have characters to traverse298Please respect copyright.PENANAAKBvobUaci
// while the word more than one letter, do this function298Please respect copyright.PENANAk1bFWenNCD
while (start < end) {298Please respect copyright.PENANAQGaoLlys4Z
// Find the leftmost vowel298Please respect copyright.PENANAQonFoHeFQ6
// while start 少於 string length() 同時 [start] 不是vowel,return start ++298Please respect copyright.PENANAaWURk42Ivx
while (start < s.length () && !isVowel(sChar[start])) {298Please respect copyright.PENANAei0V0FLCoH
start++;298Please respect copyright.PENANAVtva2uoN0o
}298Please respect copyright.PENANAh7YSw2c80m
// Find the rightmost vowel298Please respect copyright.PENANAP0fkI4JNYZ
// while end 大於 0 同時 [end] 不是vowel,return end --298Please respect copyright.PENANAHSTKccjeGl
while (end >= 0 && !isVowel(sChar[end])) {298Please respect copyright.PENANA3Irzr6EIfl
end--;298Please respect copyright.PENANAmhFW4ERFDB
}298Please respect copyright.PENANAkdK8L2fjrV
// Swap them if start is left of end298Please respect copyright.PENANAvCdoMZA14C
// swap function: (in what string, value 1, value 2), swap value 1 and 2298Please respect copyright.PENANALCDdujkP8p
if (start < end) {298Please respect copyright.PENANAZYe8cDgxPj
swap(sChar, start++, end--);298Please respect copyright.PENANAr0Xby5z6dj
}298Please respect copyright.PENANA7W8vzLwG3l
}298Please respect copyright.PENANAPudgIVcaM7
298Please respect copyright.PENANAIBOYqZTFQe
// Converting char array back to String298Please respect copyright.PENANAScHyD0ifwv
// 顯示新的String298Please respect copyright.PENANA385N9yE68b
return new String(sChar);298Please respect copyright.PENANApAfUH89l2i
}298Please respect copyright.PENANATSKe818eBh
};