
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.
413Please respect copyright.PENANAFWBCVb7kiM
Two Pointers
class Solution {413Please respect copyright.PENANA1Hs9EEs7ZA
// Return true if the character is a vowel (case-insensitive)413Please respect copyright.PENANAqhpuXhsdjP
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU413Please respect copyright.PENANAfqc57wldYL
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。413Please respect copyright.PENANAIqkj9jRXFB
boolean isVowel(char c) {413Please respect copyright.PENANAEXnpBCuTys
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'413Please respect copyright.PENANAr9cPoLBA0m
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';413Please respect copyright.PENANA6mXN2osuiU
}413Please respect copyright.PENANAGexLVTTPxb
413Please respect copyright.PENANAM56zqZx4rS
// Function to swap characters at index x and y413Please respect copyright.PENANA9ULN5bP0AL
void swap(char[] chars, int x, int y) {413Please respect copyright.PENANAX6DApJdpMr
char temp = chars[x];413Please respect copyright.PENANAIMvSgosGI7
chars[x] = chars[y];413Please respect copyright.PENANAfufFdS3hdx
chars[y] = temp;413Please respect copyright.PENANAACRPzIB6nT
}413Please respect copyright.PENANAPYJtTDDzQl
413Please respect copyright.PENANAAcWGQRENkr
public String reverseVowels(String s) {413Please respect copyright.PENANAARrfXSLQRM
// 設定最左的字母是[0]413Please respect copyright.PENANA3hmwiU3pYg
int start = 0;413Please respect copyright.PENANAoiOonGdqFB
// 設定最右的字母是[文字總長度-1].413Please respect copyright.PENANAH6OcSQzOKg
int end = s.length() - 1;413Please respect copyright.PENANApb09cnPrKy
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的413Please respect copyright.PENANAF8cn2yFDjZ
char[] sChar = s.toCharArray();413Please respect copyright.PENANAPGya21AvoR
413Please respect copyright.PENANAaK77Ng4Qqv
// While we still have characters to traverse413Please respect copyright.PENANARu6A2YKLh0
// while the word more than one letter, do this function413Please respect copyright.PENANAkQtopuFSbB
while (start < end) {413Please respect copyright.PENANAE8OPbNmHmH
// Find the leftmost vowel413Please respect copyright.PENANAppzVMV7Ks2
// while start 少於 string length() 同時 [start] 不是vowel,return start ++413Please respect copyright.PENANA92Qb786f62
while (start < s.length () && !isVowel(sChar[start])) {413Please respect copyright.PENANADDfTBwOM17
start++;413Please respect copyright.PENANAIb6en61m48
}413Please respect copyright.PENANAZL9trUXuiM
// Find the rightmost vowel413Please respect copyright.PENANATqztjbfSw2
// while end 大於 0 同時 [end] 不是vowel,return end --413Please respect copyright.PENANAgDC1CjjdEJ
while (end >= 0 && !isVowel(sChar[end])) {413Please respect copyright.PENANATpWKLOkZKA
end--;413Please respect copyright.PENANA29ACPYzdnH
}413Please respect copyright.PENANApzDN9TiCja
// Swap them if start is left of end413Please respect copyright.PENANAvDgOPcEnIh
// swap function: (in what string, value 1, value 2), swap value 1 and 2413Please respect copyright.PENANAq3GN3YwN7y
if (start < end) {413Please respect copyright.PENANA3mmPgi3prG
swap(sChar, start++, end--);413Please respect copyright.PENANAEote6dTLOr
}413Please respect copyright.PENANABAsDcUJifR
}413Please respect copyright.PENANABBUoytaeEO
413Please respect copyright.PENANAcNIdCeID7r
// Converting char array back to String413Please respect copyright.PENANA9zki2VPeri
// 顯示新的String413Please respect copyright.PENANAQwVUIEzZnE
return new String(sChar);413Please respect copyright.PENANAiwYZYkGiAt
}413Please respect copyright.PENANAO2ODoF74z4
};