
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.
366Please respect copyright.PENANAErqGjSSclx
Two Pointers
class Solution {366Please respect copyright.PENANAjO0nJZFVug
// Return true if the character is a vowel (case-insensitive)366Please respect copyright.PENANAmp4p93tqYA
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU366Please respect copyright.PENANALsdi9PhCYM
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。366Please respect copyright.PENANASwrWnShdCI
boolean isVowel(char c) {366Please respect copyright.PENANAAvriMIR1Sk
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'366Please respect copyright.PENANAA4qObOvjDO
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';366Please respect copyright.PENANAzOPZLcLxEN
}366Please respect copyright.PENANAsa09UmMtRn
366Please respect copyright.PENANAfInw0GrmGf
// Function to swap characters at index x and y366Please respect copyright.PENANACsLnZEKT7t
void swap(char[] chars, int x, int y) {366Please respect copyright.PENANAy18dOHuKa1
char temp = chars[x];366Please respect copyright.PENANAaRGxaoX2U2
chars[x] = chars[y];366Please respect copyright.PENANAhg6P87uQ02
chars[y] = temp;366Please respect copyright.PENANAHm8NEULUAl
}366Please respect copyright.PENANA4yXkSPAysU
366Please respect copyright.PENANAgnfQtqYJ4R
public String reverseVowels(String s) {366Please respect copyright.PENANA0471AviwKs
// 設定最左的字母是[0]366Please respect copyright.PENANA4zpd8HSSvz
int start = 0;366Please respect copyright.PENANAtEV8IQXbXO
// 設定最右的字母是[文字總長度-1].366Please respect copyright.PENANAhC2bGpv5Px
int end = s.length() - 1;366Please respect copyright.PENANAXs5BSaAiI9
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的366Please respect copyright.PENANAl88tXuKpJs
char[] sChar = s.toCharArray();366Please respect copyright.PENANARPhu3TS5Tz
366Please respect copyright.PENANAVQV6wJfIgC
// While we still have characters to traverse366Please respect copyright.PENANAphGItGaDjZ
// while the word more than one letter, do this function366Please respect copyright.PENANA1OvVBNs6vo
while (start < end) {366Please respect copyright.PENANAa4Rjzaa8R7
// Find the leftmost vowel366Please respect copyright.PENANATKC6xWCxZL
// while start 少於 string length() 同時 [start] 不是vowel,return start ++366Please respect copyright.PENANAdkIJJnsPGa
while (start < s.length () && !isVowel(sChar[start])) {366Please respect copyright.PENANAnmupR6GdVm
start++;366Please respect copyright.PENANADoHOI9T6uO
}366Please respect copyright.PENANAHuQQSPti2T
// Find the rightmost vowel366Please respect copyright.PENANANSnzYNzW2k
// while end 大於 0 同時 [end] 不是vowel,return end --366Please respect copyright.PENANACa7fB4KfEY
while (end >= 0 && !isVowel(sChar[end])) {366Please respect copyright.PENANArNYHF7NA8Q
end--;366Please respect copyright.PENANAP9LiU8JA77
}366Please respect copyright.PENANAnytX0ACpSo
// Swap them if start is left of end366Please respect copyright.PENANAhaVtG0smjZ
// swap function: (in what string, value 1, value 2), swap value 1 and 2366Please respect copyright.PENANAhBQ7mKGx2z
if (start < end) {366Please respect copyright.PENANAw7gIWocxN1
swap(sChar, start++, end--);366Please respect copyright.PENANAq2UGkDa8gy
}366Please respect copyright.PENANAqxpUySdFNl
}366Please respect copyright.PENANAhvA0vT0tPH
366Please respect copyright.PENANALsl8ZarQl0
// Converting char array back to String366Please respect copyright.PENANA2UQ8G5sq0l
// 顯示新的String366Please respect copyright.PENANAyG0agijs9B
return new String(sChar);366Please respect copyright.PENANAuyTfP9phPs
}366Please respect copyright.PENANAUjGhoDxtpJ
};