
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.
414Please respect copyright.PENANAeOxwo55ZAP
Two Pointers
class Solution {414Please respect copyright.PENANAs4REXkyNHw
// Return true if the character is a vowel (case-insensitive)414Please respect copyright.PENANA4UowCZPBX2
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU414Please respect copyright.PENANAibvxx4wxbk
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。414Please respect copyright.PENANAYcfZGXqERp
boolean isVowel(char c) {414Please respect copyright.PENANA2FExmVlSWG
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'414Please respect copyright.PENANAaagMETYm10
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';414Please respect copyright.PENANAQBpE0LWJqv
}414Please respect copyright.PENANAZe2KUN8KXD
414Please respect copyright.PENANAzuiQpUqOaL
// Function to swap characters at index x and y414Please respect copyright.PENANAmq6WjTKwPJ
void swap(char[] chars, int x, int y) {414Please respect copyright.PENANAl0Q5ocuEOk
char temp = chars[x];414Please respect copyright.PENANA0QhNnxMeGS
chars[x] = chars[y];414Please respect copyright.PENANAhF7YJaQbXZ
chars[y] = temp;414Please respect copyright.PENANAtUwVLLNXKA
}414Please respect copyright.PENANA8dA337bGxT
414Please respect copyright.PENANAPAN54Rw7sX
public String reverseVowels(String s) {414Please respect copyright.PENANAI4NtIOb6bW
// 設定最左的字母是[0]414Please respect copyright.PENANA2JHWpMcg9J
int start = 0;414Please respect copyright.PENANA9aOPRRLJvY
// 設定最右的字母是[文字總長度-1].414Please respect copyright.PENANAA5sUpJNlHB
int end = s.length() - 1;414Please respect copyright.PENANAQgOhoPQcmV
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的414Please respect copyright.PENANAk0aLKVUSiZ
char[] sChar = s.toCharArray();414Please respect copyright.PENANAQbtyRisqGG
414Please respect copyright.PENANAIUfmCk5UeC
// While we still have characters to traverse414Please respect copyright.PENANArAENcRDBlu
// while the word more than one letter, do this function414Please respect copyright.PENANAzAmwNH3iwv
while (start < end) {414Please respect copyright.PENANAOu8ELPH6lv
// Find the leftmost vowel414Please respect copyright.PENANAqr9gleQewp
// while start 少於 string length() 同時 [start] 不是vowel,return start ++414Please respect copyright.PENANAzcojdffGne
while (start < s.length () && !isVowel(sChar[start])) {414Please respect copyright.PENANAULy96YDIZu
start++;414Please respect copyright.PENANAst8YIBm2vL
}414Please respect copyright.PENANAqHgbbWeLCr
// Find the rightmost vowel414Please respect copyright.PENANAYLevbzMXbv
// while end 大於 0 同時 [end] 不是vowel,return end --414Please respect copyright.PENANAGnPEiGCBPw
while (end >= 0 && !isVowel(sChar[end])) {414Please respect copyright.PENANAkILGSp40I1
end--;414Please respect copyright.PENANAX0uCOxCGkg
}414Please respect copyright.PENANAqDtOAc47rf
// Swap them if start is left of end414Please respect copyright.PENANAOUC0zs7R7h
// swap function: (in what string, value 1, value 2), swap value 1 and 2414Please respect copyright.PENANAG8CQOrbf6m
if (start < end) {414Please respect copyright.PENANAjdoRg54I6n
swap(sChar, start++, end--);414Please respect copyright.PENANAuo24mKri5q
}414Please respect copyright.PENANApeFJc8NmS7
}414Please respect copyright.PENANAXsc0c3z48s
414Please respect copyright.PENANALGLP0ZJJAm
// Converting char array back to String414Please respect copyright.PENANAOfpV9rNIS3
// 顯示新的String414Please respect copyright.PENANAGDrMn25XFs
return new String(sChar);414Please respect copyright.PENANAkCgGn0XGYh
}414Please respect copyright.PENANAZNK6apcTE8
};