
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.
342Please respect copyright.PENANAyTX21doPJz
Two Pointers
class Solution {342Please respect copyright.PENANA8XMr1RtgFb
// Return true if the character is a vowel (case-insensitive)342Please respect copyright.PENANA9Zq1ZNP73s
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU342Please respect copyright.PENANAtAnIXbWI8v
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。342Please respect copyright.PENANA9cLksGGDq7
boolean isVowel(char c) {342Please respect copyright.PENANAi1lHjUXCum
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'342Please respect copyright.PENANAUFrbMn44s4
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';342Please respect copyright.PENANAKzBZ3Xd09M
}342Please respect copyright.PENANA5K8c754Pms
342Please respect copyright.PENANA7yWnWBC1Ad
// Function to swap characters at index x and y342Please respect copyright.PENANAaMVK0VmdsN
void swap(char[] chars, int x, int y) {342Please respect copyright.PENANAHzrYEPY6iu
char temp = chars[x];342Please respect copyright.PENANAW2Q2oNaRML
chars[x] = chars[y];342Please respect copyright.PENANAq6A9FPgHZy
chars[y] = temp;342Please respect copyright.PENANA75mwYq7SLA
}342Please respect copyright.PENANAFOuU6Ee6GW
342Please respect copyright.PENANAl5zoDppfZD
public String reverseVowels(String s) {342Please respect copyright.PENANAxpS8Fy8AaD
// 設定最左的字母是[0]342Please respect copyright.PENANAnBo4BwElM9
int start = 0;342Please respect copyright.PENANAIh1UjX4K0w
// 設定最右的字母是[文字總長度-1].342Please respect copyright.PENANAFTItTmlRAQ
int end = s.length() - 1;342Please respect copyright.PENANAyMmGhWd23i
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的342Please respect copyright.PENANALphIBARKo0
char[] sChar = s.toCharArray();342Please respect copyright.PENANAktge6Dvg2W
342Please respect copyright.PENANAS1wStOyMaf
// While we still have characters to traverse342Please respect copyright.PENANAwQYudChE9l
// while the word more than one letter, do this function342Please respect copyright.PENANAAMzxGtmW9z
while (start < end) {342Please respect copyright.PENANAfXpsUGalkV
// Find the leftmost vowel342Please respect copyright.PENANAjtrXUVyoAO
// while start 少於 string length() 同時 [start] 不是vowel,return start ++342Please respect copyright.PENANAdvgmZ5Ir5O
while (start < s.length () && !isVowel(sChar[start])) {342Please respect copyright.PENANAsAcaW58YpL
start++;342Please respect copyright.PENANAP5yKEf6KuV
}342Please respect copyright.PENANAJ2uoU6IUAE
// Find the rightmost vowel342Please respect copyright.PENANAvyVHFucNQM
// while end 大於 0 同時 [end] 不是vowel,return end --342Please respect copyright.PENANAXwnYLP7Qus
while (end >= 0 && !isVowel(sChar[end])) {342Please respect copyright.PENANAMYmpar4Ueh
end--;342Please respect copyright.PENANAXhIOAV31IE
}342Please respect copyright.PENANAAomMoBbmMm
// Swap them if start is left of end342Please respect copyright.PENANAE09MXtBygv
// swap function: (in what string, value 1, value 2), swap value 1 and 2342Please respect copyright.PENANAVp6a5IV8WO
if (start < end) {342Please respect copyright.PENANASfjiM7dqmt
swap(sChar, start++, end--);342Please respect copyright.PENANAIbCNYCt7eM
}342Please respect copyright.PENANAOoB89Gl6fZ
}342Please respect copyright.PENANAH8MDaF9jVz
342Please respect copyright.PENANAtUrw9Gkjtf
// Converting char array back to String342Please respect copyright.PENANAaOThPR5Nse
// 顯示新的String342Please respect copyright.PENANAQvziWNBTYQ
return new String(sChar);342Please respect copyright.PENANAKoTAzGuYHR
}342Please respect copyright.PENANA0kXJcgsUhM
};