
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.
287Please respect copyright.PENANAIxNGXmF3xh
Two Pointers
class Solution {287Please respect copyright.PENANAzFWmokmCHr
// Return true if the character is a vowel (case-insensitive)287Please respect copyright.PENANAP3m6Nl5DQm
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU287Please respect copyright.PENANAZvOdHz4AOV
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。287Please respect copyright.PENANA1pDaTpHNjo
boolean isVowel(char c) {287Please respect copyright.PENANAxduh0jBJiN
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'287Please respect copyright.PENANAhE5L82lCQj
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';287Please respect copyright.PENANAvmmNeHJdsB
}287Please respect copyright.PENANADwTaj7pT9i
287Please respect copyright.PENANAgL028rX3DL
// Function to swap characters at index x and y287Please respect copyright.PENANAGkdaul9Vve
void swap(char[] chars, int x, int y) {287Please respect copyright.PENANAvZDsRD7UtW
char temp = chars[x];287Please respect copyright.PENANAW5DHyX0Vt6
chars[x] = chars[y];287Please respect copyright.PENANAjwKx7Jl5vf
chars[y] = temp;287Please respect copyright.PENANAt5LTpbY5Rf
}287Please respect copyright.PENANALT6A8tMmHY
287Please respect copyright.PENANABMzLoYc1Dn
public String reverseVowels(String s) {287Please respect copyright.PENANAbnMkj8SvWA
// 設定最左的字母是[0]287Please respect copyright.PENANAuxZLv8Pb1X
int start = 0;287Please respect copyright.PENANAossOY9hSuz
// 設定最右的字母是[文字總長度-1].287Please respect copyright.PENANAjQkvy4KEZA
int end = s.length() - 1;287Please respect copyright.PENANAXHIQEDBX4v
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的287Please respect copyright.PENANA8biLYGcyOC
char[] sChar = s.toCharArray();287Please respect copyright.PENANAsIIXtGG6Vi
287Please respect copyright.PENANAZ9LEqsnjCn
// While we still have characters to traverse287Please respect copyright.PENANAP51CnEdbAJ
// while the word more than one letter, do this function287Please respect copyright.PENANAO6em6vIPdC
while (start < end) {287Please respect copyright.PENANATKpYIFTJRV
// Find the leftmost vowel287Please respect copyright.PENANALVbYNHFNBQ
// while start 少於 string length() 同時 [start] 不是vowel,return start ++287Please respect copyright.PENANA71aTbDGYom
while (start < s.length () && !isVowel(sChar[start])) {287Please respect copyright.PENANArGPp7PxI7p
start++;287Please respect copyright.PENANAEOyfBB47cY
}287Please respect copyright.PENANAtSLyO0qLLa
// Find the rightmost vowel287Please respect copyright.PENANAaBqGtXqXUJ
// while end 大於 0 同時 [end] 不是vowel,return end --287Please respect copyright.PENANAHe7tfkJPh1
while (end >= 0 && !isVowel(sChar[end])) {287Please respect copyright.PENANAmKLIVN0AWe
end--;287Please respect copyright.PENANAG0s6AV7n1l
}287Please respect copyright.PENANAdm0mfHpC4Q
// Swap them if start is left of end287Please respect copyright.PENANA98jM5mdrYA
// swap function: (in what string, value 1, value 2), swap value 1 and 2287Please respect copyright.PENANAL40uzEeOdv
if (start < end) {287Please respect copyright.PENANAoFXMlN8PST
swap(sChar, start++, end--);287Please respect copyright.PENANAajohwxzuF3
}287Please respect copyright.PENANAKOOdX5sZSe
}287Please respect copyright.PENANAZUxpuaWbF5
287Please respect copyright.PENANAN9bqf47xMm
// Converting char array back to String287Please respect copyright.PENANAo2A4c9abKD
// 顯示新的String287Please respect copyright.PENANAGmO71ZpUhX
return new String(sChar);287Please respect copyright.PENANAzTnRY31wF2
}287Please respect copyright.PENANAEdePbv2iqP
};