
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.
347Please respect copyright.PENANAnD0mCvvmQX
Two Pointers
class Solution {347Please respect copyright.PENANAncCpkTIpPr
// Return true if the character is a vowel (case-insensitive)347Please respect copyright.PENANA8aYS52mpYA
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU347Please respect copyright.PENANAHz2hJRJRSE
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。347Please respect copyright.PENANAJ1tudFXOWs
boolean isVowel(char c) {347Please respect copyright.PENANA1uZIJeVxHy
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'347Please respect copyright.PENANA9Ls0lEkFg5
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';347Please respect copyright.PENANAzpoFqNOBAM
}347Please respect copyright.PENANAsBYSvXbmZN
347Please respect copyright.PENANAGuz8Xaeasz
// Function to swap characters at index x and y347Please respect copyright.PENANAUYOQGwNdct
void swap(char[] chars, int x, int y) {347Please respect copyright.PENANAzmmDNzdWtI
char temp = chars[x];347Please respect copyright.PENANAEcUmnHSyDc
chars[x] = chars[y];347Please respect copyright.PENANAnoTBuLGJ2a
chars[y] = temp;347Please respect copyright.PENANANawfZnO6Er
}347Please respect copyright.PENANATeky357fsp
347Please respect copyright.PENANAkZnEB4g71Q
public String reverseVowels(String s) {347Please respect copyright.PENANApoiPLde7dP
// 設定最左的字母是[0]347Please respect copyright.PENANAoYNFSiWuz7
int start = 0;347Please respect copyright.PENANAtULJNFIF3Z
// 設定最右的字母是[文字總長度-1].347Please respect copyright.PENANAE3jxkABhW0
int end = s.length() - 1;347Please respect copyright.PENANAOTILDKTDrC
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的347Please respect copyright.PENANA9fPIFD8r2H
char[] sChar = s.toCharArray();347Please respect copyright.PENANA2y4AH1svTa
347Please respect copyright.PENANAxh3zhAbhSe
// While we still have characters to traverse347Please respect copyright.PENANA8JOVBWkoLK
// while the word more than one letter, do this function347Please respect copyright.PENANAmRm7MTK2zA
while (start < end) {347Please respect copyright.PENANAUKWNOPMtwv
// Find the leftmost vowel347Please respect copyright.PENANAEpSWBU6iZo
// while start 少於 string length() 同時 [start] 不是vowel,return start ++347Please respect copyright.PENANAXFM2L47pRG
while (start < s.length () && !isVowel(sChar[start])) {347Please respect copyright.PENANAdb7axmTm8t
start++;347Please respect copyright.PENANAI8MwbZkLvk
}347Please respect copyright.PENANAJ2eqviEr4I
// Find the rightmost vowel347Please respect copyright.PENANAY0CWjLOs8G
// while end 大於 0 同時 [end] 不是vowel,return end --347Please respect copyright.PENANAeqXIazxtch
while (end >= 0 && !isVowel(sChar[end])) {347Please respect copyright.PENANA2gxV837gjV
end--;347Please respect copyright.PENANAdg23xDagym
}347Please respect copyright.PENANAOhc79dG2if
// Swap them if start is left of end347Please respect copyright.PENANA7ss2bWIMq2
// swap function: (in what string, value 1, value 2), swap value 1 and 2347Please respect copyright.PENANAJdoiMbRxEW
if (start < end) {347Please respect copyright.PENANAJdnzEK4D7e
swap(sChar, start++, end--);347Please respect copyright.PENANAhxtRKRFXoA
}347Please respect copyright.PENANAzeOl2isG5x
}347Please respect copyright.PENANAKuWHA9F8PP
347Please respect copyright.PENANAv66kcO2O43
// Converting char array back to String347Please respect copyright.PENANAltv5vMgJo8
// 顯示新的String347Please respect copyright.PENANAoOAd11wqMV
return new String(sChar);347Please respect copyright.PENANAlVMJqJuahj
}347Please respect copyright.PENANArMzsd2Qr1B
};