
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.
363Please respect copyright.PENANA0zHGeaxiK1
Two Pointers
class Solution {363Please respect copyright.PENANA0WICWX6KhK
// Return true if the character is a vowel (case-insensitive)363Please respect copyright.PENANA1HZq3A8Oha
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU363Please respect copyright.PENANAlg5JLrX116
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。363Please respect copyright.PENANALkXq5urKCG
boolean isVowel(char c) {363Please respect copyright.PENANA2gvK0eLmVS
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'363Please respect copyright.PENANA3XrwGRgZjV
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';363Please respect copyright.PENANA4upWhI8VoI
}363Please respect copyright.PENANAGa2hjKBLZ9
363Please respect copyright.PENANAE16J7gSlkf
// Function to swap characters at index x and y363Please respect copyright.PENANAdrKdPLdygm
void swap(char[] chars, int x, int y) {363Please respect copyright.PENANA79mEAHhW6b
char temp = chars[x];363Please respect copyright.PENANALh2uwU7M8q
chars[x] = chars[y];363Please respect copyright.PENANAE2y2J456pU
chars[y] = temp;363Please respect copyright.PENANA3EveY7skwz
}363Please respect copyright.PENANA4kQ5svyLQM
363Please respect copyright.PENANA3lkD77TrJJ
public String reverseVowels(String s) {363Please respect copyright.PENANAp2UNXWghOP
// 設定最左的字母是[0]363Please respect copyright.PENANA52Xbmue1XK
int start = 0;363Please respect copyright.PENANAcGHKZiXl5I
// 設定最右的字母是[文字總長度-1].363Please respect copyright.PENANAXT0KYuYgOd
int end = s.length() - 1;363Please respect copyright.PENANAKc0dY7AIcY
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的363Please respect copyright.PENANAUIqBC0OZkQ
char[] sChar = s.toCharArray();363Please respect copyright.PENANADIc4M1JMS5
363Please respect copyright.PENANABD253mwekT
// While we still have characters to traverse363Please respect copyright.PENANAaLaN95wDlH
// while the word more than one letter, do this function363Please respect copyright.PENANAFjG75E3hsh
while (start < end) {363Please respect copyright.PENANAg4HS4FOJ0A
// Find the leftmost vowel363Please respect copyright.PENANA8OsYi9qguD
// while start 少於 string length() 同時 [start] 不是vowel,return start ++363Please respect copyright.PENANAjmb89n3WCv
while (start < s.length () && !isVowel(sChar[start])) {363Please respect copyright.PENANAY8MHZLQhaW
start++;363Please respect copyright.PENANAFxoKGwGII5
}363Please respect copyright.PENANAnhzJnURDzt
// Find the rightmost vowel363Please respect copyright.PENANAy7rdvOSsgQ
// while end 大於 0 同時 [end] 不是vowel,return end --363Please respect copyright.PENANAHLm6R1diTe
while (end >= 0 && !isVowel(sChar[end])) {363Please respect copyright.PENANASFLNZ2pLOA
end--;363Please respect copyright.PENANAJUjOPyg9de
}363Please respect copyright.PENANA4rY8lcf5JB
// Swap them if start is left of end363Please respect copyright.PENANAXKuKdphMc7
// swap function: (in what string, value 1, value 2), swap value 1 and 2363Please respect copyright.PENANAXxxXPBCAYj
if (start < end) {363Please respect copyright.PENANAVBJylTiWwz
swap(sChar, start++, end--);363Please respect copyright.PENANAuwW52cp3lj
}363Please respect copyright.PENANA74lER8xLHS
}363Please respect copyright.PENANAWakxKPRBCk
363Please respect copyright.PENANAesCDGwdVKE
// Converting char array back to String363Please respect copyright.PENANAncPGmMLO9n
// 顯示新的String363Please respect copyright.PENANAVE698JrQaw
return new String(sChar);363Please respect copyright.PENANAkZxi8Q81MD
}363Please respect copyright.PENANAWyz16d7CVx
};