
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.
261Please respect copyright.PENANA1JQ2oihJ70
Two Pointers
class Solution {261Please respect copyright.PENANA2oTlpevATU
// Return true if the character is a vowel (case-insensitive)261Please respect copyright.PENANALhS8tuPLcr
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU261Please respect copyright.PENANAHF3NcSjLEM
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。261Please respect copyright.PENANAKeUkURd2K3
boolean isVowel(char c) {261Please respect copyright.PENANALXha6cehkW
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'261Please respect copyright.PENANAH1Krpi3Dyo
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';261Please respect copyright.PENANAyoDjRwToKz
}261Please respect copyright.PENANApgx8Mctcsk
261Please respect copyright.PENANAGQDBPHbzJy
// Function to swap characters at index x and y261Please respect copyright.PENANApqxxXQdMO3
void swap(char[] chars, int x, int y) {261Please respect copyright.PENANAkVW65cWAdI
char temp = chars[x];261Please respect copyright.PENANAJKe0FDM5wM
chars[x] = chars[y];261Please respect copyright.PENANAdiZ77byUdE
chars[y] = temp;261Please respect copyright.PENANArN4quDNFsQ
}261Please respect copyright.PENANAxfnCWzKf2L
261Please respect copyright.PENANANLpAaiicFb
public String reverseVowels(String s) {261Please respect copyright.PENANAgZz6uHFSq8
// 設定最左的字母是[0]261Please respect copyright.PENANACCoRLj8MAd
int start = 0;261Please respect copyright.PENANAoYq5TcxfgE
// 設定最右的字母是[文字總長度-1].261Please respect copyright.PENANAVj5VzRYFH5
int end = s.length() - 1;261Please respect copyright.PENANAOs2EQvSyfi
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的261Please respect copyright.PENANAGnu70CJHhT
char[] sChar = s.toCharArray();261Please respect copyright.PENANAlUfpFLI6vA
261Please respect copyright.PENANA77PJNcMd1E
// While we still have characters to traverse261Please respect copyright.PENANA9rWjvMxtoX
// while the word more than one letter, do this function261Please respect copyright.PENANAwPn6NggSIp
while (start < end) {261Please respect copyright.PENANAzK6CPIP9e0
// Find the leftmost vowel261Please respect copyright.PENANABmjILW0BNC
// while start 少於 string length() 同時 [start] 不是vowel,return start ++261Please respect copyright.PENANASPSfeJWdHJ
while (start < s.length () && !isVowel(sChar[start])) {261Please respect copyright.PENANAN4Fzjo0DVz
start++;261Please respect copyright.PENANAQYvathIBNd
}261Please respect copyright.PENANAsBFiClT8UX
// Find the rightmost vowel261Please respect copyright.PENANAOxuO99g1xU
// while end 大於 0 同時 [end] 不是vowel,return end --261Please respect copyright.PENANAG5obliiq0a
while (end >= 0 && !isVowel(sChar[end])) {261Please respect copyright.PENANAo00XaCZp5C
end--;261Please respect copyright.PENANAPjSaWn15Az
}261Please respect copyright.PENANADH0uTtT3eX
// Swap them if start is left of end261Please respect copyright.PENANAnypWDmkPNm
// swap function: (in what string, value 1, value 2), swap value 1 and 2261Please respect copyright.PENANAgYTL2obJ22
if (start < end) {261Please respect copyright.PENANAuRNfLn3QZI
swap(sChar, start++, end--);261Please respect copyright.PENANAaVUeNvohPd
}261Please respect copyright.PENANAF3i8JUKR5x
}261Please respect copyright.PENANAroF2qH4Zd9
261Please respect copyright.PENANA9bb3oJxXzb
// Converting char array back to String261Please respect copyright.PENANAwlm7w0GU0c
// 顯示新的String261Please respect copyright.PENANAxHwUx1mWJU
return new String(sChar);261Please respect copyright.PENANAsJQPf6cgNQ
}261Please respect copyright.PENANACxAO2psUkk
};