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.
180Please respect copyright.PENANAvYbphzjEFj
Two Pointers
class Solution {180Please respect copyright.PENANA0DvdF9pehy
// Return true if the character is a vowel (case-insensitive)180Please respect copyright.PENANAteXj5plwuz
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU180Please respect copyright.PENANAGmeKefseDG
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。180Please respect copyright.PENANAX0mLgdeekF
boolean isVowel(char c) {180Please respect copyright.PENANAYpiijQUyGR
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'180Please respect copyright.PENANAy10AQnjQbY
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';180Please respect copyright.PENANANr7JRgl2j1
}180Please respect copyright.PENANANeOcxmzCb5
180Please respect copyright.PENANAroJAxnDryU
// Function to swap characters at index x and y180Please respect copyright.PENANAPy9En7vheY
void swap(char[] chars, int x, int y) {180Please respect copyright.PENANAewzbdSdrFK
char temp = chars[x];180Please respect copyright.PENANA6xbX4dqb5K
chars[x] = chars[y];180Please respect copyright.PENANA5Ol2CBszXb
chars[y] = temp;180Please respect copyright.PENANAXDro9z2eat
}180Please respect copyright.PENANATByRW7a8lC
180Please respect copyright.PENANAsxucQbbZs9
public String reverseVowels(String s) {180Please respect copyright.PENANALetUFM43eJ
// 設定最左的字母是[0]180Please respect copyright.PENANAmfmx0Z2yYA
int start = 0;180Please respect copyright.PENANASOM9auTyTv
// 設定最右的字母是[文字總長度-1].180Please respect copyright.PENANAxHC2xHSKXq
int end = s.length() - 1;180Please respect copyright.PENANA7N1oRn3Qxq
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的180Please respect copyright.PENANAklZjDnUJIB
char[] sChar = s.toCharArray();180Please respect copyright.PENANAZdB3TbBLNZ
180Please respect copyright.PENANAj4UT4vkTPr
// While we still have characters to traverse180Please respect copyright.PENANANGhIoMwh12
// while the word more than one letter, do this function180Please respect copyright.PENANAnON2ROPbUd
while (start < end) {180Please respect copyright.PENANAU0mtiKvm0S
// Find the leftmost vowel180Please respect copyright.PENANAcBA2OuTFZJ
// while start 少於 string length() 同時 [start] 不是vowel,return start ++180Please respect copyright.PENANAvF4aH3yzcU
while (start < s.length () && !isVowel(sChar[start])) {180Please respect copyright.PENANAb8TM7y9dAP
start++;180Please respect copyright.PENANAMw4BaLKf6A
}180Please respect copyright.PENANA7lDo3muCZJ
// Find the rightmost vowel180Please respect copyright.PENANAxtwQcfLzuN
// while end 大於 0 同時 [end] 不是vowel,return end --180Please respect copyright.PENANAklHyEzw4qr
while (end >= 0 && !isVowel(sChar[end])) {180Please respect copyright.PENANAH8YGISLlka
end--;180Please respect copyright.PENANAmMONDVfHqC
}180Please respect copyright.PENANApDiTAvhXYE
// Swap them if start is left of end180Please respect copyright.PENANAxDjuAmn75n
// swap function: (in what string, value 1, value 2), swap value 1 and 2180Please respect copyright.PENANAxjjrbvdJnm
if (start < end) {180Please respect copyright.PENANADeYcoCNcWN
swap(sChar, start++, end--);180Please respect copyright.PENANAjMGDiHXIvy
}180Please respect copyright.PENANAR8oPTsSKmi
}180Please respect copyright.PENANAetQfquXUYj
180Please respect copyright.PENANAj658w5W7jg
// Converting char array back to String180Please respect copyright.PENANARvxXifLmdu
// 顯示新的String180Please respect copyright.PENANAXngtUsWAeX
return new String(sChar);180Please respect copyright.PENANAtmzEo98E7Z
}180Please respect copyright.PENANAmzo6Fr7Z3j
};