
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.
401Please respect copyright.PENANAixa0ycS2Nc
Two Pointers
class Solution {401Please respect copyright.PENANAR6TOsohqVp
// Return true if the character is a vowel (case-insensitive)401Please respect copyright.PENANA3SlhdT0HYV
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU401Please respect copyright.PENANAAcb0H7FLtB
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。401Please respect copyright.PENANAgHEw9vzjDB
boolean isVowel(char c) {401Please respect copyright.PENANAkV4VYXSEbC
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'401Please respect copyright.PENANAoD5vJlQGrv
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';401Please respect copyright.PENANA06rE1LQmfM
}401Please respect copyright.PENANAXdMf7tKZGm
401Please respect copyright.PENANAWlt8vMPMYW
// Function to swap characters at index x and y401Please respect copyright.PENANA8jMCieF5Zb
void swap(char[] chars, int x, int y) {401Please respect copyright.PENANAL2I9mVgzHd
char temp = chars[x];401Please respect copyright.PENANAhO01H1NF5t
chars[x] = chars[y];401Please respect copyright.PENANAi4H48RzRuI
chars[y] = temp;401Please respect copyright.PENANAScp513idwG
}401Please respect copyright.PENANAJR6mVbpLOq
401Please respect copyright.PENANAokcZ65au54
public String reverseVowels(String s) {401Please respect copyright.PENANA7h4cJ9SKpx
// 設定最左的字母是[0]401Please respect copyright.PENANA1IaFxHDDbC
int start = 0;401Please respect copyright.PENANAKAdjB8uKhR
// 設定最右的字母是[文字總長度-1].401Please respect copyright.PENANAHaQ0mhjsX5
int end = s.length() - 1;401Please respect copyright.PENANAM75f3Ub1z3
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的401Please respect copyright.PENANAAOpsUYu3pn
char[] sChar = s.toCharArray();401Please respect copyright.PENANAHWOlsG1vnF
401Please respect copyright.PENANAaGMrBSewN6
// While we still have characters to traverse401Please respect copyright.PENANA1m4iy1IoGt
// while the word more than one letter, do this function401Please respect copyright.PENANARlXEKgYPZ8
while (start < end) {401Please respect copyright.PENANAkyAPeTT7XJ
// Find the leftmost vowel401Please respect copyright.PENANAJpUtajoHkP
// while start 少於 string length() 同時 [start] 不是vowel,return start ++401Please respect copyright.PENANAB9KVFqgxsy
while (start < s.length () && !isVowel(sChar[start])) {401Please respect copyright.PENANA05ds32Mwut
start++;401Please respect copyright.PENANAEbn8c6QHP2
}401Please respect copyright.PENANA8XNZxgnoXh
// Find the rightmost vowel401Please respect copyright.PENANA1OWvr6UdnB
// while end 大於 0 同時 [end] 不是vowel,return end --401Please respect copyright.PENANA6hXUKx5v0I
while (end >= 0 && !isVowel(sChar[end])) {401Please respect copyright.PENANAO89cn0tkMZ
end--;401Please respect copyright.PENANAeO1Xti68nN
}401Please respect copyright.PENANAzhpfaBYZuI
// Swap them if start is left of end401Please respect copyright.PENANAEVcJju35TM
// swap function: (in what string, value 1, value 2), swap value 1 and 2401Please respect copyright.PENANAqy6BvsysEJ
if (start < end) {401Please respect copyright.PENANArutpCqY4tc
swap(sChar, start++, end--);401Please respect copyright.PENANAC6UnAkyhiY
}401Please respect copyright.PENANAjQaOH3MhhC
}401Please respect copyright.PENANAxPD7jm59Nd
401Please respect copyright.PENANAJPipxOcOJS
// Converting char array back to String401Please respect copyright.PENANAJUlWTCXJWF
// 顯示新的String401Please respect copyright.PENANADpd5DZsa97
return new String(sChar);401Please respect copyright.PENANA78dUe6NCtc
}401Please respect copyright.PENANAOsoDvsvPwG
};