
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.
285Please respect copyright.PENANA5pIN0nfvNF
Two Pointers
class Solution {285Please respect copyright.PENANAsYzTE9ykID
// Return true if the character is a vowel (case-insensitive)285Please respect copyright.PENANALPKGXW8dmQ
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU285Please respect copyright.PENANAfe7qPZJoKG
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。285Please respect copyright.PENANAjeqGOlP1Sv
boolean isVowel(char c) {285Please respect copyright.PENANAa1y7Chmmhg
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'285Please respect copyright.PENANAYyguC4SvrN
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';285Please respect copyright.PENANAVHNNZCxTR0
}285Please respect copyright.PENANA0V5OeWNdto
285Please respect copyright.PENANAwzSG4vMbTO
// Function to swap characters at index x and y285Please respect copyright.PENANAVdgHScSlVD
void swap(char[] chars, int x, int y) {285Please respect copyright.PENANAzbghr8BUUP
char temp = chars[x];285Please respect copyright.PENANArujS4jdJnf
chars[x] = chars[y];285Please respect copyright.PENANADc3cKBj2Kc
chars[y] = temp;285Please respect copyright.PENANAX6BI5YEYDR
}285Please respect copyright.PENANADuUuuVXbnL
285Please respect copyright.PENANA7xRxjciwAT
public String reverseVowels(String s) {285Please respect copyright.PENANAxUuoAVtYHM
// 設定最左的字母是[0]285Please respect copyright.PENANAQCYQZPnBpz
int start = 0;285Please respect copyright.PENANA20dbWqO9fR
// 設定最右的字母是[文字總長度-1].285Please respect copyright.PENANA6Ca3ndVvIt
int end = s.length() - 1;285Please respect copyright.PENANA6dcy1GymRh
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的285Please respect copyright.PENANAINZNj7EveX
char[] sChar = s.toCharArray();285Please respect copyright.PENANApTsPBD3zZ4
285Please respect copyright.PENANAGmNqyU1H68
// While we still have characters to traverse285Please respect copyright.PENANA2qwi9AYhe8
// while the word more than one letter, do this function285Please respect copyright.PENANAetanVzsdtb
while (start < end) {285Please respect copyright.PENANAL7pKbT1NRG
// Find the leftmost vowel285Please respect copyright.PENANAm2hciR8TBO
// while start 少於 string length() 同時 [start] 不是vowel,return start ++285Please respect copyright.PENANAS47BMqyqM7
while (start < s.length () && !isVowel(sChar[start])) {285Please respect copyright.PENANAlCM0SeqkMo
start++;285Please respect copyright.PENANAWNjOmZz8UL
}285Please respect copyright.PENANAIkYqwodg1r
// Find the rightmost vowel285Please respect copyright.PENANAPT3f41cVqQ
// while end 大於 0 同時 [end] 不是vowel,return end --285Please respect copyright.PENANAG4KbVS5SOZ
while (end >= 0 && !isVowel(sChar[end])) {285Please respect copyright.PENANAW2J1GAjiCZ
end--;285Please respect copyright.PENANArEH3cJXNVM
}285Please respect copyright.PENANAZQyyQUvTV8
// Swap them if start is left of end285Please respect copyright.PENANA8tst5s9Pbj
// swap function: (in what string, value 1, value 2), swap value 1 and 2285Please respect copyright.PENANAYm4IP2netw
if (start < end) {285Please respect copyright.PENANAMqbhyikN1k
swap(sChar, start++, end--);285Please respect copyright.PENANAstYfsgZyIb
}285Please respect copyright.PENANAHHNyoARPew
}285Please respect copyright.PENANAwh6JBGDpdL
285Please respect copyright.PENANAWcEYm7Lf0J
// Converting char array back to String285Please respect copyright.PENANAKwj0FI9lke
// 顯示新的String285Please respect copyright.PENANA4H9pAIRoj1
return new String(sChar);285Please respect copyright.PENANArzVshBfaAS
}285Please respect copyright.PENANAJq0qVJA9eE
};