
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.
379Please respect copyright.PENANAX0hKhpleQN
Two Pointers
class Solution {379Please respect copyright.PENANATRQTBbY74K
// Return true if the character is a vowel (case-insensitive)379Please respect copyright.PENANAAjDDQLLoBy
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU379Please respect copyright.PENANAtorjwIsQNA
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。379Please respect copyright.PENANAXzq7Lt1Olt
boolean isVowel(char c) {379Please respect copyright.PENANAf0P7bLtlOE
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'379Please respect copyright.PENANAgYb78gEc5g
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';379Please respect copyright.PENANAxqNmcNDtX6
}379Please respect copyright.PENANA3TVK1K4lkN
379Please respect copyright.PENANACtEn2cd4wr
// Function to swap characters at index x and y379Please respect copyright.PENANAVkvmB6tT1f
void swap(char[] chars, int x, int y) {379Please respect copyright.PENANAu0ASsZsbyp
char temp = chars[x];379Please respect copyright.PENANA8EArPH0vsD
chars[x] = chars[y];379Please respect copyright.PENANA7Rfp8WglwQ
chars[y] = temp;379Please respect copyright.PENANACQzy3kuIfa
}379Please respect copyright.PENANAP5EOXTpygZ
379Please respect copyright.PENANAVF2QDHkHah
public String reverseVowels(String s) {379Please respect copyright.PENANAvuPWJoSEkP
// 設定最左的字母是[0]379Please respect copyright.PENANAnQD0VnRzRX
int start = 0;379Please respect copyright.PENANA92NVTA54rc
// 設定最右的字母是[文字總長度-1].379Please respect copyright.PENANA6CC2tPcMUg
int end = s.length() - 1;379Please respect copyright.PENANAwHvyUoJDsQ
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的379Please respect copyright.PENANA5DEROTEJsp
char[] sChar = s.toCharArray();379Please respect copyright.PENANAk0IPtVJuqv
379Please respect copyright.PENANAGIqzHzrNRP
// While we still have characters to traverse379Please respect copyright.PENANAFO0qtL9qgO
// while the word more than one letter, do this function379Please respect copyright.PENANA4WOVF55BXM
while (start < end) {379Please respect copyright.PENANAIAqxjA2xN8
// Find the leftmost vowel379Please respect copyright.PENANA3WJRNzT1ff
// while start 少於 string length() 同時 [start] 不是vowel,return start ++379Please respect copyright.PENANAxnSdKQUmzP
while (start < s.length () && !isVowel(sChar[start])) {379Please respect copyright.PENANA1nY9eK9rOG
start++;379Please respect copyright.PENANAa0y7CpkNJX
}379Please respect copyright.PENANA7cRNiUporH
// Find the rightmost vowel379Please respect copyright.PENANASs9hlRiLGE
// while end 大於 0 同時 [end] 不是vowel,return end --379Please respect copyright.PENANAKvlaGhbp0P
while (end >= 0 && !isVowel(sChar[end])) {379Please respect copyright.PENANAO6IMMgpW5p
end--;379Please respect copyright.PENANA1IxpMEUenW
}379Please respect copyright.PENANA3cx0dNa2Dg
// Swap them if start is left of end379Please respect copyright.PENANAYiNTOVMcfy
// swap function: (in what string, value 1, value 2), swap value 1 and 2379Please respect copyright.PENANA7UwdtqlrFr
if (start < end) {379Please respect copyright.PENANAAQF8fHlWRs
swap(sChar, start++, end--);379Please respect copyright.PENANAynkjIoFldI
}379Please respect copyright.PENANAYk5Ce1pw7N
}379Please respect copyright.PENANABWP0ymuf5R
379Please respect copyright.PENANA28CpBfKpk8
// Converting char array back to String379Please respect copyright.PENANAsgdrFpvxpT
// 顯示新的String379Please respect copyright.PENANAGcIiGYuPQ2
return new String(sChar);379Please respect copyright.PENANALdCKa8zf8z
}379Please respect copyright.PENANAiBEmMAoE5a
};