
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.
378Please respect copyright.PENANA4mLnNUpnp3
Two Pointers
class Solution {378Please respect copyright.PENANAGrrgDsEWZm
// Return true if the character is a vowel (case-insensitive)378Please respect copyright.PENANAiIY6lzuOfc
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU378Please respect copyright.PENANAWB6zbSFCpU
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。378Please respect copyright.PENANAYiQI47jZGl
boolean isVowel(char c) {378Please respect copyright.PENANAnlViCUqLY4
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'378Please respect copyright.PENANAqi3uXoLYNO
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';378Please respect copyright.PENANAlz8f4WVF2g
}378Please respect copyright.PENANA0CChEEWg3F
378Please respect copyright.PENANAu8EFt5V3sY
// Function to swap characters at index x and y378Please respect copyright.PENANAOaIUg1Znsf
void swap(char[] chars, int x, int y) {378Please respect copyright.PENANAIuFmxFr0Fo
char temp = chars[x];378Please respect copyright.PENANA0jKMjj164e
chars[x] = chars[y];378Please respect copyright.PENANAD0CsGYl1Kq
chars[y] = temp;378Please respect copyright.PENANAxL4Uk8NsKC
}378Please respect copyright.PENANAwt7kBxQV8w
378Please respect copyright.PENANAOu8GL0CJeP
public String reverseVowels(String s) {378Please respect copyright.PENANAW2G39bN0l3
// 設定最左的字母是[0]378Please respect copyright.PENANA3PAymwha3D
int start = 0;378Please respect copyright.PENANAJLXeaBoivT
// 設定最右的字母是[文字總長度-1].378Please respect copyright.PENANAzwKOJGNl1H
int end = s.length() - 1;378Please respect copyright.PENANANJrBnCKUKu
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的378Please respect copyright.PENANAC6ySYPgJZf
char[] sChar = s.toCharArray();378Please respect copyright.PENANA7G1opP9kOF
378Please respect copyright.PENANATfZdp9lEQl
// While we still have characters to traverse378Please respect copyright.PENANApEFMZjHWPr
// while the word more than one letter, do this function378Please respect copyright.PENANAM5BsUCJi6E
while (start < end) {378Please respect copyright.PENANAhlX7HcLAmg
// Find the leftmost vowel378Please respect copyright.PENANAqoVGBYbnze
// while start 少於 string length() 同時 [start] 不是vowel,return start ++378Please respect copyright.PENANAemhneiKOrn
while (start < s.length () && !isVowel(sChar[start])) {378Please respect copyright.PENANAUPHT3HfsKB
start++;378Please respect copyright.PENANAeevo16rXCr
}378Please respect copyright.PENANAsBbdoormsi
// Find the rightmost vowel378Please respect copyright.PENANAIkKUu9QdI8
// while end 大於 0 同時 [end] 不是vowel,return end --378Please respect copyright.PENANAeg5tZMoF0r
while (end >= 0 && !isVowel(sChar[end])) {378Please respect copyright.PENANANfN2H5qZJb
end--;378Please respect copyright.PENANAH5Vih3DUMQ
}378Please respect copyright.PENANA7zbdR1gCkX
// Swap them if start is left of end378Please respect copyright.PENANAxrZZM4S4sD
// swap function: (in what string, value 1, value 2), swap value 1 and 2378Please respect copyright.PENANAYsIW2OM4Sr
if (start < end) {378Please respect copyright.PENANAQvOamBgpCb
swap(sChar, start++, end--);378Please respect copyright.PENANAPcYkdcZT5M
}378Please respect copyright.PENANAkyujWX6BN2
}378Please respect copyright.PENANApfmJYSrgiW
378Please respect copyright.PENANAXrGlsYltHj
// Converting char array back to String378Please respect copyright.PENANAgMt3PpkVhL
// 顯示新的String378Please respect copyright.PENANAQ4wN4BGL4Z
return new String(sChar);378Please respect copyright.PENANAaPq0TnYRwA
}378Please respect copyright.PENANAqLQjOXvFXC
};