
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.
373Please respect copyright.PENANAgYfqcyq405
Two Pointers
class Solution {373Please respect copyright.PENANAYbL4mMO6FL
// Return true if the character is a vowel (case-insensitive)373Please respect copyright.PENANALvgDkpagB3
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU373Please respect copyright.PENANAcL6Y1Vh8J0
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。373Please respect copyright.PENANADmL0DqVLf8
boolean isVowel(char c) {373Please respect copyright.PENANAQgfL5JNFNQ
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'373Please respect copyright.PENANAdKEX5VqWm5
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';373Please respect copyright.PENANAlk6UobvGN2
}373Please respect copyright.PENANAY0hJhKJF3Z
373Please respect copyright.PENANAaMxISC9hoj
// Function to swap characters at index x and y373Please respect copyright.PENANAZTuYxhB8mt
void swap(char[] chars, int x, int y) {373Please respect copyright.PENANAc9kv5i3x9O
char temp = chars[x];373Please respect copyright.PENANAVsHHHJU3qe
chars[x] = chars[y];373Please respect copyright.PENANAUmf3XE1xEj
chars[y] = temp;373Please respect copyright.PENANA9i1L2YU12R
}373Please respect copyright.PENANAszmAVAsDlD
373Please respect copyright.PENANAq7yaRlQAlL
public String reverseVowels(String s) {373Please respect copyright.PENANAoGdlxgSvSZ
// 設定最左的字母是[0]373Please respect copyright.PENANA1w4IOjanSx
int start = 0;373Please respect copyright.PENANAjYYEncdmGk
// 設定最右的字母是[文字總長度-1].373Please respect copyright.PENANAWEbNXYLxfj
int end = s.length() - 1;373Please respect copyright.PENANAfvvUcX00Bn
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的373Please respect copyright.PENANAb91JbolmX0
char[] sChar = s.toCharArray();373Please respect copyright.PENANAGPz0rO8yZ1
373Please respect copyright.PENANAIG7qkFXCvg
// While we still have characters to traverse373Please respect copyright.PENANAJJ4Jz6pgCq
// while the word more than one letter, do this function373Please respect copyright.PENANAnt1CcKFlYG
while (start < end) {373Please respect copyright.PENANAyfw3ib7zYH
// Find the leftmost vowel373Please respect copyright.PENANAVvKalRj3TG
// while start 少於 string length() 同時 [start] 不是vowel,return start ++373Please respect copyright.PENANAxlrOewcnwU
while (start < s.length () && !isVowel(sChar[start])) {373Please respect copyright.PENANAOhgHYCIiTn
start++;373Please respect copyright.PENANAAvt9d6NstP
}373Please respect copyright.PENANAhYZuJtr9Mv
// Find the rightmost vowel373Please respect copyright.PENANAj2AQUd5qzs
// while end 大於 0 同時 [end] 不是vowel,return end --373Please respect copyright.PENANAh5ansU7vJg
while (end >= 0 && !isVowel(sChar[end])) {373Please respect copyright.PENANAoFTfUHcQ8N
end--;373Please respect copyright.PENANAv3YyYiBDNt
}373Please respect copyright.PENANAG0ZGFa6QaX
// Swap them if start is left of end373Please respect copyright.PENANADn5lC7DIdH
// swap function: (in what string, value 1, value 2), swap value 1 and 2373Please respect copyright.PENANA0PzWFYP9q0
if (start < end) {373Please respect copyright.PENANAkl8yax6cnQ
swap(sChar, start++, end--);373Please respect copyright.PENANAnMBbG3NCiK
}373Please respect copyright.PENANAvOUuTNaRCS
}373Please respect copyright.PENANAnabbg75FNn
373Please respect copyright.PENANAYYVG6chjlI
// Converting char array back to String373Please respect copyright.PENANAebe6KHjP0T
// 顯示新的String373Please respect copyright.PENANAzcFshojFT3
return new String(sChar);373Please respect copyright.PENANAl8cWWMT7kl
}373Please respect copyright.PENANA9JH0Mq3VnE
};