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.
572Please respect copyright.PENANAOyCiWalt8z
Two Pointers
class Solution {572Please respect copyright.PENANAQtw117rZJK
// Return true if the character is a vowel (case-insensitive)572Please respect copyright.PENANAZsu4azrzUc
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU572Please respect copyright.PENANA805H1yskiA
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。572Please respect copyright.PENANAdwtsqnyrT3
boolean isVowel(char c) {572Please respect copyright.PENANAUbC18uWcs4
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'572Please respect copyright.PENANAchQFPYR2Ha
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';572Please respect copyright.PENANA65CEVi2alH
}572Please respect copyright.PENANA3mYripFiq9
572Please respect copyright.PENANAEBG8XJZhrc
// Function to swap characters at index x and y572Please respect copyright.PENANAoC50Rr0Npo
void swap(char[] chars, int x, int y) {572Please respect copyright.PENANAFV3IF3qLSF
char temp = chars[x];572Please respect copyright.PENANAjnoGp1GCAI
chars[x] = chars[y];572Please respect copyright.PENANAmR4YmsHYLi
chars[y] = temp;572Please respect copyright.PENANAATR9Lq5L36
}572Please respect copyright.PENANAOOxSrgyo7v
572Please respect copyright.PENANAmM3nCdhWMG
public String reverseVowels(String s) {572Please respect copyright.PENANAlKJvOndn36
// 設定最左的字母是[0]572Please respect copyright.PENANAKU5Q40KD9b
int start = 0;572Please respect copyright.PENANA4xUvrBRYHg
// 設定最右的字母是[文字總長度-1].572Please respect copyright.PENANAG4DAPnAD2I
int end = s.length() - 1;572Please respect copyright.PENANA2rm7UJXT0V
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的572Please respect copyright.PENANA4iS6QdnOk3
char[] sChar = s.toCharArray();572Please respect copyright.PENANATR4nzyCkkI
572Please respect copyright.PENANABMRGRurEKc
// While we still have characters to traverse572Please respect copyright.PENANAE35BtIDaFU
// while the word more than one letter, do this function572Please respect copyright.PENANAipQn7QPSEI
while (start < end) {572Please respect copyright.PENANAm2fp9azDH0
// Find the leftmost vowel572Please respect copyright.PENANA4SndB9ctLT
// while start 少於 string length() 同時 [start] 不是vowel,return start ++572Please respect copyright.PENANA45QZD8Sa8Z
while (start < s.length () && !isVowel(sChar[start])) {572Please respect copyright.PENANAgfF0IoZn74
start++;572Please respect copyright.PENANA6jZcMmfyCg
}572Please respect copyright.PENANA4lqL3ewqgR
// Find the rightmost vowel572Please respect copyright.PENANAX0IzKhscBw
// while end 大於 0 同時 [end] 不是vowel,return end --572Please respect copyright.PENANAIKvwkWNcZH
while (end >= 0 && !isVowel(sChar[end])) {572Please respect copyright.PENANAK1HxW69bZW
end--;572Please respect copyright.PENANAKwWt4Vdp49
}572Please respect copyright.PENANAUkLnexipmM
// Swap them if start is left of end572Please respect copyright.PENANAFjliK72PZr
// swap function: (in what string, value 1, value 2), swap value 1 and 2572Please respect copyright.PENANAfymgoz7Nvj
if (start < end) {572Please respect copyright.PENANA8NyeHH7GpN
swap(sChar, start++, end--);572Please respect copyright.PENANAHDcOOMhd9u
}572Please respect copyright.PENANACrPbGdALRy
}572Please respect copyright.PENANAJYGfiFPxJt
572Please respect copyright.PENANAsDO1evvRCA
// Converting char array back to String572Please respect copyright.PENANAkHXblXbRfy
// 顯示新的String572Please respect copyright.PENANAEDEfiPuHKV
return new String(sChar);572Please respect copyright.PENANA0CvbLzm3Ma
}572Please respect copyright.PENANAWz5q5lukTs
};


