
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.
299Please respect copyright.PENANAT52D7bpz68
Two Pointers
class Solution {299Please respect copyright.PENANAAYX7mf2I7Y
// Return true if the character is a vowel (case-insensitive)299Please respect copyright.PENANAohcw3LUC7i
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU299Please respect copyright.PENANA6agALFQMxX
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。299Please respect copyright.PENANAqZHPzTpdpq
boolean isVowel(char c) {299Please respect copyright.PENANAQ3dg4EZHi3
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'299Please respect copyright.PENANAlf2idhWvdb
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';299Please respect copyright.PENANAuDqRujOV5Z
}299Please respect copyright.PENANA8RWZK3lxP6
299Please respect copyright.PENANAbVxyjstkHH
// Function to swap characters at index x and y299Please respect copyright.PENANAxjAIRkTH2X
void swap(char[] chars, int x, int y) {299Please respect copyright.PENANAw0rfLTHR3G
char temp = chars[x];299Please respect copyright.PENANAK9KVVmvDWV
chars[x] = chars[y];299Please respect copyright.PENANARbBstGKE16
chars[y] = temp;299Please respect copyright.PENANAY8ybkkorKR
}299Please respect copyright.PENANAOXhjdNvDG5
299Please respect copyright.PENANAfqJRp6mmYg
public String reverseVowels(String s) {299Please respect copyright.PENANAw4UWHfvm8h
// 設定最左的字母是[0]299Please respect copyright.PENANAnrXKN2pbUP
int start = 0;299Please respect copyright.PENANAKU1pMJlnnQ
// 設定最右的字母是[文字總長度-1].299Please respect copyright.PENANAW4tWivcJ59
int end = s.length() - 1;299Please respect copyright.PENANAb1cIf5b6nk
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的299Please respect copyright.PENANA8GzSh3Ki48
char[] sChar = s.toCharArray();299Please respect copyright.PENANAkmvRlz6voM
299Please respect copyright.PENANAsVeh943GwT
// While we still have characters to traverse299Please respect copyright.PENANA8muVwWXYrY
// while the word more than one letter, do this function299Please respect copyright.PENANAb4zjENO2st
while (start < end) {299Please respect copyright.PENANAzuKoI2XB4F
// Find the leftmost vowel299Please respect copyright.PENANAaHjRIYhxE4
// while start 少於 string length() 同時 [start] 不是vowel,return start ++299Please respect copyright.PENANAQtTmybGlqB
while (start < s.length () && !isVowel(sChar[start])) {299Please respect copyright.PENANAoaPKT8dFua
start++;299Please respect copyright.PENANAQ78uzmISps
}299Please respect copyright.PENANAD798oGoO2V
// Find the rightmost vowel299Please respect copyright.PENANATVSBrmLmUP
// while end 大於 0 同時 [end] 不是vowel,return end --299Please respect copyright.PENANABltRr5ShQP
while (end >= 0 && !isVowel(sChar[end])) {299Please respect copyright.PENANAbpia9twQPr
end--;299Please respect copyright.PENANAExe7pOBqoV
}299Please respect copyright.PENANAc727R87799
// Swap them if start is left of end299Please respect copyright.PENANAIyIyTQvtdx
// swap function: (in what string, value 1, value 2), swap value 1 and 2299Please respect copyright.PENANAFCRYqpFBFU
if (start < end) {299Please respect copyright.PENANAQVN7DtEWhK
swap(sChar, start++, end--);299Please respect copyright.PENANA5jHiUkx7OV
}299Please respect copyright.PENANAVdEOCLOZiV
}299Please respect copyright.PENANAfYr7VehPHT
299Please respect copyright.PENANAoirFgYMGdR
// Converting char array back to String299Please respect copyright.PENANASbnzbCPMSq
// 顯示新的String299Please respect copyright.PENANAB9BY1sYH4u
return new String(sChar);299Please respect copyright.PENANAZUELjfvJjX
}299Please respect copyright.PENANAiwZ8cP4T5g
};