
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.
249Please respect copyright.PENANAtUTnY0O9hi
Two Pointers
class Solution {249Please respect copyright.PENANAwsKwlskVxH
// Return true if the character is a vowel (case-insensitive)249Please respect copyright.PENANAkgVihd5ueM
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU249Please respect copyright.PENANAwi3eOoS7sh
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。249Please respect copyright.PENANA7sWZz4pAOm
boolean isVowel(char c) {249Please respect copyright.PENANAiHu4g4Ubwl
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'249Please respect copyright.PENANANe7QCINWVh
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';249Please respect copyright.PENANAWyO3QJqmyd
}249Please respect copyright.PENANAcTJ7cBFACp
249Please respect copyright.PENANAGSyftnNHIq
// Function to swap characters at index x and y249Please respect copyright.PENANAkMDcNz2J0Z
void swap(char[] chars, int x, int y) {249Please respect copyright.PENANABY6xjV7UAd
char temp = chars[x];249Please respect copyright.PENANA0s74IIPzup
chars[x] = chars[y];249Please respect copyright.PENANAwbqIN9snzX
chars[y] = temp;249Please respect copyright.PENANADHAzKfxEN9
}249Please respect copyright.PENANALWsKnMDvCp
249Please respect copyright.PENANAnEmrqyxTDV
public String reverseVowels(String s) {249Please respect copyright.PENANAUsOOCBPjHL
// 設定最左的字母是[0]249Please respect copyright.PENANAWPhluV2VXl
int start = 0;249Please respect copyright.PENANAycB4Z4SA09
// 設定最右的字母是[文字總長度-1].249Please respect copyright.PENANAK6hPMUb1lY
int end = s.length() - 1;249Please respect copyright.PENANA7sjoKOGdeh
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的249Please respect copyright.PENANAnnu5thkA6J
char[] sChar = s.toCharArray();249Please respect copyright.PENANA6Wca6yUSQ4
249Please respect copyright.PENANAissu00eKUj
// While we still have characters to traverse249Please respect copyright.PENANAZQnyse2GEF
// while the word more than one letter, do this function249Please respect copyright.PENANAej48W0yZ8v
while (start < end) {249Please respect copyright.PENANAnqkIe0qsRS
// Find the leftmost vowel249Please respect copyright.PENANAIaNHTsGGHe
// while start 少於 string length() 同時 [start] 不是vowel,return start ++249Please respect copyright.PENANAQQRxTglm5Y
while (start < s.length () && !isVowel(sChar[start])) {249Please respect copyright.PENANAoleMCQ4OKW
start++;249Please respect copyright.PENANAh2rgC6eMNd
}249Please respect copyright.PENANACOA1jGmRXc
// Find the rightmost vowel249Please respect copyright.PENANAT8uTdkjznW
// while end 大於 0 同時 [end] 不是vowel,return end --249Please respect copyright.PENANA8LnjKmGf6e
while (end >= 0 && !isVowel(sChar[end])) {249Please respect copyright.PENANAPtPAx9Y3Wi
end--;249Please respect copyright.PENANAYClPW53hrL
}249Please respect copyright.PENANApW5MIACZEY
// Swap them if start is left of end249Please respect copyright.PENANAkKGiTx6XBN
// swap function: (in what string, value 1, value 2), swap value 1 and 2249Please respect copyright.PENANA55yEO5eucQ
if (start < end) {249Please respect copyright.PENANAn5X3yy9fVQ
swap(sChar, start++, end--);249Please respect copyright.PENANAHhkSusXjaU
}249Please respect copyright.PENANAFfqhpj8bmF
}249Please respect copyright.PENANA5D7YvpNbZz
249Please respect copyright.PENANAsSjZTFsNEM
// Converting char array back to String249Please respect copyright.PENANAfO0RPeYaXZ
// 顯示新的String249Please respect copyright.PENANAoxXHvKeytO
return new String(sChar);249Please respect copyright.PENANAowAg3FWThH
}249Please respect copyright.PENANAjBdBDjKVno
};