
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.
353Please respect copyright.PENANAXkPpoWrFBI
Two Pointers
class Solution {353Please respect copyright.PENANATu9X1G6yqD
// Return true if the character is a vowel (case-insensitive)353Please respect copyright.PENANA8aUFOUOHfl
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU353Please respect copyright.PENANAxfDWdUoXvH
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。353Please respect copyright.PENANAjpCLyl3Nid
boolean isVowel(char c) {353Please respect copyright.PENANAFpqdNP4MGU
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'353Please respect copyright.PENANA1tzHFeGu4D
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';353Please respect copyright.PENANAi0hS5JCM5K
}353Please respect copyright.PENANAiJ04pVND6y
353Please respect copyright.PENANAsVJDOfwKDo
// Function to swap characters at index x and y353Please respect copyright.PENANApMM47VGWVO
void swap(char[] chars, int x, int y) {353Please respect copyright.PENANAKv5JeuNKw2
char temp = chars[x];353Please respect copyright.PENANAb1bdm26mD3
chars[x] = chars[y];353Please respect copyright.PENANAR1Q6LgHqAz
chars[y] = temp;353Please respect copyright.PENANACR9GNVOiux
}353Please respect copyright.PENANA4PKRVAKolx
353Please respect copyright.PENANAuXnITJC8zm
public String reverseVowels(String s) {353Please respect copyright.PENANAvEKGLMJi1E
// 設定最左的字母是[0]353Please respect copyright.PENANAqRcTPN3xOP
int start = 0;353Please respect copyright.PENANA6T1kltCsFn
// 設定最右的字母是[文字總長度-1].353Please respect copyright.PENANApFrUmInvOo
int end = s.length() - 1;353Please respect copyright.PENANA7OmbuO6VKe
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的353Please respect copyright.PENANAQ8rIptCehT
char[] sChar = s.toCharArray();353Please respect copyright.PENANAfgFoD7BX6l
353Please respect copyright.PENANAj70OkLISS7
// While we still have characters to traverse353Please respect copyright.PENANA2Ar9n4F4jc
// while the word more than one letter, do this function353Please respect copyright.PENANA1CSfIS48JQ
while (start < end) {353Please respect copyright.PENANA7GB93cFrn9
// Find the leftmost vowel353Please respect copyright.PENANASuSdOfwC40
// while start 少於 string length() 同時 [start] 不是vowel,return start ++353Please respect copyright.PENANAnGrhsXkiTB
while (start < s.length () && !isVowel(sChar[start])) {353Please respect copyright.PENANApsUKNo67Xe
start++;353Please respect copyright.PENANAJcKCG81z2K
}353Please respect copyright.PENANACpT4hpspPA
// Find the rightmost vowel353Please respect copyright.PENANAMo0qJsD8Vg
// while end 大於 0 同時 [end] 不是vowel,return end --353Please respect copyright.PENANArdJT5j2iOs
while (end >= 0 && !isVowel(sChar[end])) {353Please respect copyright.PENANAHyVkmxhm5h
end--;353Please respect copyright.PENANAvNuYAKddB9
}353Please respect copyright.PENANASTAe1cTZ6g
// Swap them if start is left of end353Please respect copyright.PENANA0QQF739Q0r
// swap function: (in what string, value 1, value 2), swap value 1 and 2353Please respect copyright.PENANAV48wLeLFlV
if (start < end) {353Please respect copyright.PENANA2qDEz1RIaI
swap(sChar, start++, end--);353Please respect copyright.PENANAN24oY1qrON
}353Please respect copyright.PENANA1hCpLsuMfI
}353Please respect copyright.PENANA23ZQpBigR8
353Please respect copyright.PENANAez6OdEF0yG
// Converting char array back to String353Please respect copyright.PENANAuayxwyTCrT
// 顯示新的String353Please respect copyright.PENANAvWmkwgq9pK
return new String(sChar);353Please respect copyright.PENANA1H9WLYlbFl
}353Please respect copyright.PENANANmwxxJC7Ca
};