
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.
400Please respect copyright.PENANA4wQ4L658mk
Two Pointers
class Solution {400Please respect copyright.PENANABi0lkXGxEW
// Return true if the character is a vowel (case-insensitive)400Please respect copyright.PENANAYKGdQ156H0
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU400Please respect copyright.PENANAr6IcUQXeW6
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。400Please respect copyright.PENANAcoAuQjFVeD
boolean isVowel(char c) {400Please respect copyright.PENANAzBbfENzb9v
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'400Please respect copyright.PENANAbnMlj8Eigo
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';400Please respect copyright.PENANAwW9BBFJWa9
}400Please respect copyright.PENANAbt00JjWoxy
400Please respect copyright.PENANAqAz2RKOPZk
// Function to swap characters at index x and y400Please respect copyright.PENANAGLm6CgKvv7
void swap(char[] chars, int x, int y) {400Please respect copyright.PENANAE4mExEjCuK
char temp = chars[x];400Please respect copyright.PENANAvjHiGYkai3
chars[x] = chars[y];400Please respect copyright.PENANAejcrDA9bRa
chars[y] = temp;400Please respect copyright.PENANA8QeahFfGti
}400Please respect copyright.PENANAViNUmZDi7o
400Please respect copyright.PENANAfvvjPhgxY1
public String reverseVowels(String s) {400Please respect copyright.PENANA4uJHirsnZR
// 設定最左的字母是[0]400Please respect copyright.PENANAmpAgkI85Qk
int start = 0;400Please respect copyright.PENANAIhq2NgYQyr
// 設定最右的字母是[文字總長度-1].400Please respect copyright.PENANAyZR01Y29M0
int end = s.length() - 1;400Please respect copyright.PENANAyTaAnNxBx2
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的400Please respect copyright.PENANAGTRMLPh4lv
char[] sChar = s.toCharArray();400Please respect copyright.PENANAcPTLm7KUmR
400Please respect copyright.PENANAjTBGokpDgy
// While we still have characters to traverse400Please respect copyright.PENANA6x1emiTHw7
// while the word more than one letter, do this function400Please respect copyright.PENANAGw3bH9i3mY
while (start < end) {400Please respect copyright.PENANAOAxFNdPyZe
// Find the leftmost vowel400Please respect copyright.PENANAPxdQ1U3Wua
// while start 少於 string length() 同時 [start] 不是vowel,return start ++400Please respect copyright.PENANAcsxjNTEFV9
while (start < s.length () && !isVowel(sChar[start])) {400Please respect copyright.PENANAdCSRWicyo6
start++;400Please respect copyright.PENANAiYcD1N3Zcy
}400Please respect copyright.PENANAzwqIDYZmNY
// Find the rightmost vowel400Please respect copyright.PENANAfdPTdOuUbh
// while end 大於 0 同時 [end] 不是vowel,return end --400Please respect copyright.PENANAScIkZtbK57
while (end >= 0 && !isVowel(sChar[end])) {400Please respect copyright.PENANAEpcBndp7Gn
end--;400Please respect copyright.PENANAPHiEa0bdz0
}400Please respect copyright.PENANAkIoxW3zXyf
// Swap them if start is left of end400Please respect copyright.PENANA4i68RUYNf2
// swap function: (in what string, value 1, value 2), swap value 1 and 2400Please respect copyright.PENANAYAcPXknK1C
if (start < end) {400Please respect copyright.PENANAAm6DLWvUO0
swap(sChar, start++, end--);400Please respect copyright.PENANApqvOTorxgY
}400Please respect copyright.PENANAXu1alguyN1
}400Please respect copyright.PENANAppZo33qGFo
400Please respect copyright.PENANA3PxLYesXqY
// Converting char array back to String400Please respect copyright.PENANA2DgpqZcbem
// 顯示新的String400Please respect copyright.PENANAuQbJTlQ9eu
return new String(sChar);400Please respect copyright.PENANAYrjo5GaLm4
}400Please respect copyright.PENANAuURPbNjvNC
};