
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.
312Please respect copyright.PENANAAQeJb6aDY9
Two Pointers
class Solution {312Please respect copyright.PENANAd4p1sWClWD
// Return true if the character is a vowel (case-insensitive)312Please respect copyright.PENANAUalBeHvwqk
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU312Please respect copyright.PENANApCYclWId3R
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。312Please respect copyright.PENANA30QEEJZk8z
boolean isVowel(char c) {312Please respect copyright.PENANArZt4nhLIjY
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'312Please respect copyright.PENANALiIPWHjuZ9
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';312Please respect copyright.PENANAm1d2JfCQkn
}312Please respect copyright.PENANAihrvEqdTEl
312Please respect copyright.PENANA0vTy4m23sv
// Function to swap characters at index x and y312Please respect copyright.PENANAXgKo8FF0Mf
void swap(char[] chars, int x, int y) {312Please respect copyright.PENANA7ySPAOdRyl
char temp = chars[x];312Please respect copyright.PENANAij9F7HhEjC
chars[x] = chars[y];312Please respect copyright.PENANA9BK6xoFZm3
chars[y] = temp;312Please respect copyright.PENANAP2gODi6NWt
}312Please respect copyright.PENANAXne9X7yKUl
312Please respect copyright.PENANAcn2l50flJi
public String reverseVowels(String s) {312Please respect copyright.PENANALflDAk61gZ
// 設定最左的字母是[0]312Please respect copyright.PENANArkwTHNTjbm
int start = 0;312Please respect copyright.PENANAtQNXREQSZY
// 設定最右的字母是[文字總長度-1].312Please respect copyright.PENANAOWlMpKDSlM
int end = s.length() - 1;312Please respect copyright.PENANASa62cFbtrF
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的312Please respect copyright.PENANA5biISQOScK
char[] sChar = s.toCharArray();312Please respect copyright.PENANAGWmt040evz
312Please respect copyright.PENANAA7G5n3BPny
// While we still have characters to traverse312Please respect copyright.PENANAiDB9giOwkx
// while the word more than one letter, do this function312Please respect copyright.PENANAPEfz2Iwjf0
while (start < end) {312Please respect copyright.PENANAHey83vtlBW
// Find the leftmost vowel312Please respect copyright.PENANAZ481C5nerl
// while start 少於 string length() 同時 [start] 不是vowel,return start ++312Please respect copyright.PENANAnTqcIFsWNO
while (start < s.length () && !isVowel(sChar[start])) {312Please respect copyright.PENANAxXI2480573
start++;312Please respect copyright.PENANAuwyoE8mXEU
}312Please respect copyright.PENANALIs0KxO83M
// Find the rightmost vowel312Please respect copyright.PENANA78gDVTTYLp
// while end 大於 0 同時 [end] 不是vowel,return end --312Please respect copyright.PENANA4ohKsu4dWV
while (end >= 0 && !isVowel(sChar[end])) {312Please respect copyright.PENANANOG8PFLOS8
end--;312Please respect copyright.PENANANbhxYmim0M
}312Please respect copyright.PENANA3Au58w9ZxG
// Swap them if start is left of end312Please respect copyright.PENANAb2gDL84PCU
// swap function: (in what string, value 1, value 2), swap value 1 and 2312Please respect copyright.PENANAMfHUmTZ6R1
if (start < end) {312Please respect copyright.PENANAyAsZ1nZGvu
swap(sChar, start++, end--);312Please respect copyright.PENANAm1k7e0wt0n
}312Please respect copyright.PENANA5ViZplYEMY
}312Please respect copyright.PENANAGamQVBRRe0
312Please respect copyright.PENANAY8OIakyUPs
// Converting char array back to String312Please respect copyright.PENANAUBimfBhFJ0
// 顯示新的String312Please respect copyright.PENANA1z7I3GptKf
return new String(sChar);312Please respect copyright.PENANA5hMwxrTc1G
}312Please respect copyright.PENANA9jj1V7mMMl
};