
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.
311Please respect copyright.PENANA9fjug6xMgw
Two Pointers
class Solution {311Please respect copyright.PENANA6SkaUvAjWr
// Return true if the character is a vowel (case-insensitive)311Please respect copyright.PENANALjjcC0dJvl
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU311Please respect copyright.PENANAm9TIUiGZAh
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。311Please respect copyright.PENANA3NvkpiIIwW
boolean isVowel(char c) {311Please respect copyright.PENANAcfVAR3gwXz
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'311Please respect copyright.PENANAhDPyW3xMLn
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';311Please respect copyright.PENANAO10ZLojh0P
}311Please respect copyright.PENANAK5svPWqp3j
311Please respect copyright.PENANAgeW4B7Ffwz
// Function to swap characters at index x and y311Please respect copyright.PENANAQEFQ2JwX5t
void swap(char[] chars, int x, int y) {311Please respect copyright.PENANAgMDHcSNFh4
char temp = chars[x];311Please respect copyright.PENANAjTWyfwrq9d
chars[x] = chars[y];311Please respect copyright.PENANASP07Vq1LxZ
chars[y] = temp;311Please respect copyright.PENANABoOnBGaiZy
}311Please respect copyright.PENANAklSIOk5sxz
311Please respect copyright.PENANAJsj1Vof2NP
public String reverseVowels(String s) {311Please respect copyright.PENANARnlPMitBsh
// 設定最左的字母是[0]311Please respect copyright.PENANAfHASHoztZC
int start = 0;311Please respect copyright.PENANAKIgD91GzYf
// 設定最右的字母是[文字總長度-1].311Please respect copyright.PENANAk5smy1PreO
int end = s.length() - 1;311Please respect copyright.PENANAIdp6aLRdUm
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的311Please respect copyright.PENANAXqgE9lg7wn
char[] sChar = s.toCharArray();311Please respect copyright.PENANAReZhzRwtgp
311Please respect copyright.PENANABfORPqwAB8
// While we still have characters to traverse311Please respect copyright.PENANAd4uHRV8PjR
// while the word more than one letter, do this function311Please respect copyright.PENANAiS19Mqz6FE
while (start < end) {311Please respect copyright.PENANAqfxNYOO5LP
// Find the leftmost vowel311Please respect copyright.PENANAkokXzMvTys
// while start 少於 string length() 同時 [start] 不是vowel,return start ++311Please respect copyright.PENANADQw7rk3qJH
while (start < s.length () && !isVowel(sChar[start])) {311Please respect copyright.PENANAsoqhMLF0o1
start++;311Please respect copyright.PENANA6CbQ27BXvK
}311Please respect copyright.PENANAJuLJBzAFe4
// Find the rightmost vowel311Please respect copyright.PENANAjpQLB2hFZ8
// while end 大於 0 同時 [end] 不是vowel,return end --311Please respect copyright.PENANAViruOFuT5v
while (end >= 0 && !isVowel(sChar[end])) {311Please respect copyright.PENANACDFqG1I3Ui
end--;311Please respect copyright.PENANAieoba6dRyr
}311Please respect copyright.PENANAdTyp0v6gWk
// Swap them if start is left of end311Please respect copyright.PENANAycyqG45dnd
// swap function: (in what string, value 1, value 2), swap value 1 and 2311Please respect copyright.PENANA7BN4SBbgGQ
if (start < end) {311Please respect copyright.PENANATGLykcvQim
swap(sChar, start++, end--);311Please respect copyright.PENANA9HF3uzde2w
}311Please respect copyright.PENANAoqu1csaw51
}311Please respect copyright.PENANAvGMLOFxjr2
311Please respect copyright.PENANArexAvlGXiy
// Converting char array back to String311Please respect copyright.PENANADDgfZQaKWf
// 顯示新的String311Please respect copyright.PENANAJT2lRLze9i
return new String(sChar);311Please respect copyright.PENANAknYDhcYYPH
}311Please respect copyright.PENANA7SxFbCmNCi
};