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.
573Please respect copyright.PENANAxLhpSKQUIy
Two Pointers
class Solution {573Please respect copyright.PENANAGmuOn89k6L
// Return true if the character is a vowel (case-insensitive)573Please respect copyright.PENANAFwnOGMIDQd
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU573Please respect copyright.PENANA5ijimU5Z36
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。573Please respect copyright.PENANAY4jUMRrCCH
boolean isVowel(char c) {573Please respect copyright.PENANAkm273tJNbk
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'573Please respect copyright.PENANA6tpaci61fu
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';573Please respect copyright.PENANAqO3HtbvG4b
}573Please respect copyright.PENANAFtVtWYRmiU
573Please respect copyright.PENANAsS4wBrYPhL
// Function to swap characters at index x and y573Please respect copyright.PENANASipF2oPZdO
void swap(char[] chars, int x, int y) {573Please respect copyright.PENANAR85TJyehKF
char temp = chars[x];573Please respect copyright.PENANA0onCV2PYqM
chars[x] = chars[y];573Please respect copyright.PENANAiWhFAZVDdd
chars[y] = temp;573Please respect copyright.PENANAsWtApgDqxB
}573Please respect copyright.PENANAs9R9Qf8IT5
573Please respect copyright.PENANAruKLMkgzuq
public String reverseVowels(String s) {573Please respect copyright.PENANANfOyLR5Lga
// 設定最左的字母是[0]573Please respect copyright.PENANAKyra9c1auM
int start = 0;573Please respect copyright.PENANA2Fwjj82FeQ
// 設定最右的字母是[文字總長度-1].573Please respect copyright.PENANApYuLTIcruF
int end = s.length() - 1;573Please respect copyright.PENANAFsLEeHzkxc
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的573Please respect copyright.PENANA5fKnVi93db
char[] sChar = s.toCharArray();573Please respect copyright.PENANA8cSpijsFI4
573Please respect copyright.PENANANM40SK0lpa
// While we still have characters to traverse573Please respect copyright.PENANA8wVURQhs6o
// while the word more than one letter, do this function573Please respect copyright.PENANApK9pYqTWfc
while (start < end) {573Please respect copyright.PENANA6eL3zvtXJf
// Find the leftmost vowel573Please respect copyright.PENANAuDul6owcIj
// while start 少於 string length() 同時 [start] 不是vowel,return start ++573Please respect copyright.PENANAiJ8aSkBu3X
while (start < s.length () && !isVowel(sChar[start])) {573Please respect copyright.PENANAt3QFzXRjgM
start++;573Please respect copyright.PENANAjKGzKHNmkH
}573Please respect copyright.PENANAv25vFI1H1a
// Find the rightmost vowel573Please respect copyright.PENANAGkaRRKkCgO
// while end 大於 0 同時 [end] 不是vowel,return end --573Please respect copyright.PENANAYao4bFayNM
while (end >= 0 && !isVowel(sChar[end])) {573Please respect copyright.PENANA1Im0xTYLmV
end--;573Please respect copyright.PENANAggACf9jTL3
}573Please respect copyright.PENANA7gVb1qZoEq
// Swap them if start is left of end573Please respect copyright.PENANATbjA29524d
// swap function: (in what string, value 1, value 2), swap value 1 and 2573Please respect copyright.PENANAmMxh5pY85L
if (start < end) {573Please respect copyright.PENANAfo5TRD3F1p
swap(sChar, start++, end--);573Please respect copyright.PENANAdGnNeor1xB
}573Please respect copyright.PENANAmUtW1A1HAE
}573Please respect copyright.PENANAs6Z5ztTtEn
573Please respect copyright.PENANAPD5Okl2SyS
// Converting char array back to String573Please respect copyright.PENANA28fleqcYvo
// 顯示新的String573Please respect copyright.PENANAIo6f7v2U6j
return new String(sChar);573Please respect copyright.PENANAfHGiMnzf5W
}573Please respect copyright.PENANAkR0Pa8XodF
};


