
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.
382Please respect copyright.PENANAP6G1lwiKhY
Two Pointers
class Solution {382Please respect copyright.PENANAILcQL0WPe3
// Return true if the character is a vowel (case-insensitive)382Please respect copyright.PENANAEWMSb9akMr
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU382Please respect copyright.PENANA9dIwkUx1Kz
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。382Please respect copyright.PENANAEXA0SbcDHO
boolean isVowel(char c) {382Please respect copyright.PENANAPf9yCQv3iL
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'382Please respect copyright.PENANAm6tOlLyvVL
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';382Please respect copyright.PENANAiFbaKxUsnL
}382Please respect copyright.PENANA26pZIhGksE
382Please respect copyright.PENANAtQ9YhPocEx
// Function to swap characters at index x and y382Please respect copyright.PENANAADCakztzdG
void swap(char[] chars, int x, int y) {382Please respect copyright.PENANAmXwvqclnQD
char temp = chars[x];382Please respect copyright.PENANATKxVLuSUOO
chars[x] = chars[y];382Please respect copyright.PENANAce9x59ge85
chars[y] = temp;382Please respect copyright.PENANAM1ZMAX1GcX
}382Please respect copyright.PENANA9Jg1dRb6fM
382Please respect copyright.PENANAizkEXMvdtd
public String reverseVowels(String s) {382Please respect copyright.PENANA55hk0kwr6M
// 設定最左的字母是[0]382Please respect copyright.PENANA9PN65rbBqc
int start = 0;382Please respect copyright.PENANANqmvwjFXrd
// 設定最右的字母是[文字總長度-1].382Please respect copyright.PENANAtl4bNrDxKG
int end = s.length() - 1;382Please respect copyright.PENANAEIrVtl8jJ1
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的382Please respect copyright.PENANAh7cA3Xxunf
char[] sChar = s.toCharArray();382Please respect copyright.PENANAYhdmYU3e7v
382Please respect copyright.PENANAcVBLNGX9hD
// While we still have characters to traverse382Please respect copyright.PENANAePiPes7yXj
// while the word more than one letter, do this function382Please respect copyright.PENANAWaNOMtoc08
while (start < end) {382Please respect copyright.PENANAR6fRvoNYBV
// Find the leftmost vowel382Please respect copyright.PENANAbu3tXPelEb
// while start 少於 string length() 同時 [start] 不是vowel,return start ++382Please respect copyright.PENANARPZx4WYt0f
while (start < s.length () && !isVowel(sChar[start])) {382Please respect copyright.PENANAL7hXs6J0Ep
start++;382Please respect copyright.PENANAnCKrG0Wklk
}382Please respect copyright.PENANAfq1izu7Amb
// Find the rightmost vowel382Please respect copyright.PENANAuu8EwyLbNY
// while end 大於 0 同時 [end] 不是vowel,return end --382Please respect copyright.PENANAY9yR2niR4g
while (end >= 0 && !isVowel(sChar[end])) {382Please respect copyright.PENANAXcGmagEnsX
end--;382Please respect copyright.PENANAVUgNRNnvKl
}382Please respect copyright.PENANAA4mMIdnvmt
// Swap them if start is left of end382Please respect copyright.PENANAAxcpmpDt6I
// swap function: (in what string, value 1, value 2), swap value 1 and 2382Please respect copyright.PENANAQKZd1EmFuD
if (start < end) {382Please respect copyright.PENANAYUDwcNH7jo
swap(sChar, start++, end--);382Please respect copyright.PENANAYkq1nmVwFp
}382Please respect copyright.PENANAP5sxjYiaVx
}382Please respect copyright.PENANAzjp2zY46Tc
382Please respect copyright.PENANAkfHKYIrHqE
// Converting char array back to String382Please respect copyright.PENANAfMkK2jzQyx
// 顯示新的String382Please respect copyright.PENANAbPoHaqSwOx
return new String(sChar);382Please respect copyright.PENANAdICEhHG29u
}382Please respect copyright.PENANA7hnZO6V4NN
};