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.
220Please respect copyright.PENANAnckWCLTxq5
Two Pointers
class Solution {220Please respect copyright.PENANAonlwMuf1LW
// Return true if the character is a vowel (case-insensitive)220Please respect copyright.PENANACcX2yBd00k
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU220Please respect copyright.PENANA2fvCGvGORs
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。220Please respect copyright.PENANA3k9pPziG4g
boolean isVowel(char c) {220Please respect copyright.PENANAxGlBDQB9kW
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'220Please respect copyright.PENANAOstYmet26E
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';220Please respect copyright.PENANAHxohDWLkgH
}220Please respect copyright.PENANAfPakIMTNsl
220Please respect copyright.PENANAAYp25DbVZN
// Function to swap characters at index x and y220Please respect copyright.PENANAhglLcv2AZu
void swap(char[] chars, int x, int y) {220Please respect copyright.PENANAxcphUZ0vyX
char temp = chars[x];220Please respect copyright.PENANAEbqtboltJQ
chars[x] = chars[y];220Please respect copyright.PENANAFKCNbQfUmG
chars[y] = temp;220Please respect copyright.PENANA368dwGZbDk
}220Please respect copyright.PENANA08r2VEKRrR
220Please respect copyright.PENANAJYVV6WRnvF
public String reverseVowels(String s) {220Please respect copyright.PENANAfWmrp3fL1a
// 設定最左的字母是[0]220Please respect copyright.PENANAts86dCmhgp
int start = 0;220Please respect copyright.PENANAv4xCXiLyrz
// 設定最右的字母是[文字總長度-1].220Please respect copyright.PENANALpGxfU5SQx
int end = s.length() - 1;220Please respect copyright.PENANA0gl10rcyAt
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的220Please respect copyright.PENANAaJErxUjIpC
char[] sChar = s.toCharArray();220Please respect copyright.PENANAWizgs8aNDT
220Please respect copyright.PENANAV1ube6mKXw
// While we still have characters to traverse220Please respect copyright.PENANAB0ljSeivBx
// while the word more than one letter, do this function220Please respect copyright.PENANAH9hSkEWz59
while (start < end) {220Please respect copyright.PENANA4pJBDcjvPo
// Find the leftmost vowel220Please respect copyright.PENANAp2oVwvqioQ
// while start 少於 string length() 同時 [start] 不是vowel,return start ++220Please respect copyright.PENANAK5PAg6FaIP
while (start < s.length () && !isVowel(sChar[start])) {220Please respect copyright.PENANAycDGoWN7v0
start++;220Please respect copyright.PENANADQyDMPBK0L
}220Please respect copyright.PENANA6EeepcUu8E
// Find the rightmost vowel220Please respect copyright.PENANAUYIdMlnoLr
// while end 大於 0 同時 [end] 不是vowel,return end --220Please respect copyright.PENANAsYBpvWTWYq
while (end >= 0 && !isVowel(sChar[end])) {220Please respect copyright.PENANAvAcivrKSHO
end--;220Please respect copyright.PENANAHc6MJ0MDPC
}220Please respect copyright.PENANAurcC4oY3OX
// Swap them if start is left of end220Please respect copyright.PENANA86oq3DZTWw
// swap function: (in what string, value 1, value 2), swap value 1 and 2220Please respect copyright.PENANAcvbairevLm
if (start < end) {220Please respect copyright.PENANAtIjCBJEX4f
swap(sChar, start++, end--);220Please respect copyright.PENANASoOARhDF1I
}220Please respect copyright.PENANAEZS2lG9V0h
}220Please respect copyright.PENANATBHWx5DoSG
220Please respect copyright.PENANANxJCT4kuXn
// Converting char array back to String220Please respect copyright.PENANAP3VHjF32cZ
// 顯示新的String220Please respect copyright.PENANAyjJXaFCj81
return new String(sChar);220Please respect copyright.PENANAx0L1VDkuAl
}220Please respect copyright.PENANAnUg6WZB8XR
};