
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.
380Please respect copyright.PENANARYB0GvQSQd
Two Pointers
class Solution {380Please respect copyright.PENANAax4XnU4FQE
// Return true if the character is a vowel (case-insensitive)380Please respect copyright.PENANAYGhkbt5G4s
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU380Please respect copyright.PENANAtxfwwaQ3MN
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。380Please respect copyright.PENANAC6X75noJKw
boolean isVowel(char c) {380Please respect copyright.PENANApI7lZVo7OC
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'380Please respect copyright.PENANAhQ6Y1lApYS
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';380Please respect copyright.PENANA93ewuS8C1B
}380Please respect copyright.PENANAmpWMkpzfrm
380Please respect copyright.PENANAp9MTjuLozJ
// Function to swap characters at index x and y380Please respect copyright.PENANAnvO1d2hr2m
void swap(char[] chars, int x, int y) {380Please respect copyright.PENANAtLVZQULCJ9
char temp = chars[x];380Please respect copyright.PENANALBw9eRqErj
chars[x] = chars[y];380Please respect copyright.PENANAsj2sCj0Tfr
chars[y] = temp;380Please respect copyright.PENANAp9ADwpTUR3
}380Please respect copyright.PENANAowjUwHoyx7
380Please respect copyright.PENANAJZbqjFLHpB
public String reverseVowels(String s) {380Please respect copyright.PENANAgTLsah1pDm
// 設定最左的字母是[0]380Please respect copyright.PENANAguS0xkMrIC
int start = 0;380Please respect copyright.PENANAeDZxcpF4yp
// 設定最右的字母是[文字總長度-1].380Please respect copyright.PENANAeoFSOv98LT
int end = s.length() - 1;380Please respect copyright.PENANAg6vHMFFkN9
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的380Please respect copyright.PENANAUJuNh4vcvv
char[] sChar = s.toCharArray();380Please respect copyright.PENANAzYRnPCkpJX
380Please respect copyright.PENANAn52OTyrCzG
// While we still have characters to traverse380Please respect copyright.PENANAzI3QQRePME
// while the word more than one letter, do this function380Please respect copyright.PENANAaW9KH3cDRO
while (start < end) {380Please respect copyright.PENANAg69bXDssJQ
// Find the leftmost vowel380Please respect copyright.PENANAQUjYVpbLgn
// while start 少於 string length() 同時 [start] 不是vowel,return start ++380Please respect copyright.PENANAmMpdiCuzUt
while (start < s.length () && !isVowel(sChar[start])) {380Please respect copyright.PENANAR9mRbPyMiV
start++;380Please respect copyright.PENANAJBQeanwLHr
}380Please respect copyright.PENANAlzDKpzXZPk
// Find the rightmost vowel380Please respect copyright.PENANAxC5L62OobA
// while end 大於 0 同時 [end] 不是vowel,return end --380Please respect copyright.PENANA3txrjpcaUj
while (end >= 0 && !isVowel(sChar[end])) {380Please respect copyright.PENANAnbwZ9cIAsW
end--;380Please respect copyright.PENANAOlhX4Z4PNk
}380Please respect copyright.PENANA9Ie07wzTpV
// Swap them if start is left of end380Please respect copyright.PENANA8c2bpFTCO0
// swap function: (in what string, value 1, value 2), swap value 1 and 2380Please respect copyright.PENANA03UsECppDd
if (start < end) {380Please respect copyright.PENANAZLjgMh0MxR
swap(sChar, start++, end--);380Please respect copyright.PENANAsoslu9Tzwe
}380Please respect copyright.PENANAdm54EUE9Wo
}380Please respect copyright.PENANAvcT70z3XPn
380Please respect copyright.PENANAVjp5zSzSgz
// Converting char array back to String380Please respect copyright.PENANAwMlNRZFL9U
// 顯示新的String380Please respect copyright.PENANAX9aLnHP96K
return new String(sChar);380Please respect copyright.PENANA2e0YQU4L6f
}380Please respect copyright.PENANAZx7LbSisfC
};