
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.
340Please respect copyright.PENANAAs3NVLj0qt
Two Pointers
class Solution {340Please respect copyright.PENANAhjpCR5PnwG
// Return true if the character is a vowel (case-insensitive)340Please respect copyright.PENANALAIT5aqUWg
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU340Please respect copyright.PENANAzuTf5cvV90
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。340Please respect copyright.PENANAyqGpwuXxjs
boolean isVowel(char c) {340Please respect copyright.PENANAfmvmlV5WbR
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'340Please respect copyright.PENANAPY2HBCWctZ
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';340Please respect copyright.PENANAwB4I6TcPZh
}340Please respect copyright.PENANAhxfLGCqdpc
340Please respect copyright.PENANA8dyX7ys4MM
// Function to swap characters at index x and y340Please respect copyright.PENANAOiOQSUR4PP
void swap(char[] chars, int x, int y) {340Please respect copyright.PENANA6BWPySlnJ9
char temp = chars[x];340Please respect copyright.PENANAUTnSJlgjTZ
chars[x] = chars[y];340Please respect copyright.PENANAgkM4dmbsse
chars[y] = temp;340Please respect copyright.PENANATRuk6z9UY0
}340Please respect copyright.PENANA6lYuPvZK0I
340Please respect copyright.PENANAXmTNC0b7tH
public String reverseVowels(String s) {340Please respect copyright.PENANA9royh4FiO0
// 設定最左的字母是[0]340Please respect copyright.PENANAzEWrx3qgXh
int start = 0;340Please respect copyright.PENANADRuBkdqQli
// 設定最右的字母是[文字總長度-1].340Please respect copyright.PENANAiFGsoW6Py3
int end = s.length() - 1;340Please respect copyright.PENANAxW19T5FTaD
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的340Please respect copyright.PENANAo6bjqIlkY3
char[] sChar = s.toCharArray();340Please respect copyright.PENANAFnOHd83c3G
340Please respect copyright.PENANAjQwi0itO7M
// While we still have characters to traverse340Please respect copyright.PENANApPI2XzSIXe
// while the word more than one letter, do this function340Please respect copyright.PENANABfvShtJZWN
while (start < end) {340Please respect copyright.PENANASAfgAiIVTk
// Find the leftmost vowel340Please respect copyright.PENANAxUfYxOfp7I
// while start 少於 string length() 同時 [start] 不是vowel,return start ++340Please respect copyright.PENANAVulOPSLvYH
while (start < s.length () && !isVowel(sChar[start])) {340Please respect copyright.PENANATbuzBkiYg2
start++;340Please respect copyright.PENANAlNXH6OhbzD
}340Please respect copyright.PENANAYpFChdP9Lb
// Find the rightmost vowel340Please respect copyright.PENANA8NQMo2kaqm
// while end 大於 0 同時 [end] 不是vowel,return end --340Please respect copyright.PENANA3jbcmIoJ3G
while (end >= 0 && !isVowel(sChar[end])) {340Please respect copyright.PENANAArKLjUMdYT
end--;340Please respect copyright.PENANAuh4IAb9zOW
}340Please respect copyright.PENANA9JlCeKdkSI
// Swap them if start is left of end340Please respect copyright.PENANAN7eBVCMd6I
// swap function: (in what string, value 1, value 2), swap value 1 and 2340Please respect copyright.PENANA4AgPqTGKN8
if (start < end) {340Please respect copyright.PENANA7yYNiDcjXL
swap(sChar, start++, end--);340Please respect copyright.PENANA2JydIliji1
}340Please respect copyright.PENANA8r1icTyClz
}340Please respect copyright.PENANAEKSwklRZUT
340Please respect copyright.PENANAbQ1TsHqij0
// Converting char array back to String340Please respect copyright.PENANAHlfmj40mqV
// 顯示新的String340Please respect copyright.PENANAyKUJzY1kfJ
return new String(sChar);340Please respect copyright.PENANAwoFklnJmEL
}340Please respect copyright.PENANAmZHuWbi6xe
};