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.
221Please respect copyright.PENANAwWE2xDQSMN
Two Pointers
class Solution {221Please respect copyright.PENANAHXZj7x3etF
// Return true if the character is a vowel (case-insensitive)221Please respect copyright.PENANA8h8KUxiSjg
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU221Please respect copyright.PENANAB2yTdZ74kC
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。221Please respect copyright.PENANAEWJB4rTVyP
boolean isVowel(char c) {221Please respect copyright.PENANAzeprFCXoer
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'221Please respect copyright.PENANAy0ziVMbHeJ
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';221Please respect copyright.PENANAZkQESnuh7m
}221Please respect copyright.PENANATyExsW33k9
221Please respect copyright.PENANABrb0eONSYl
// Function to swap characters at index x and y221Please respect copyright.PENANAvkzxvdh0RQ
void swap(char[] chars, int x, int y) {221Please respect copyright.PENANA6YNU6gBbfo
char temp = chars[x];221Please respect copyright.PENANAjnpwYFZuFu
chars[x] = chars[y];221Please respect copyright.PENANANytWMjlHyw
chars[y] = temp;221Please respect copyright.PENANAr3ybsZmR4m
}221Please respect copyright.PENANAM1XybixqCU
221Please respect copyright.PENANAMFbY5MJF4C
public String reverseVowels(String s) {221Please respect copyright.PENANA3non2Eoesf
// 設定最左的字母是[0]221Please respect copyright.PENANAgM55YIoY8W
int start = 0;221Please respect copyright.PENANAG3AAccastV
// 設定最右的字母是[文字總長度-1].221Please respect copyright.PENANAKyf1jUoSDY
int end = s.length() - 1;221Please respect copyright.PENANAtdi5XRYa8x
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的221Please respect copyright.PENANA2ca1RA8B8c
char[] sChar = s.toCharArray();221Please respect copyright.PENANAotIq7dgSz3
221Please respect copyright.PENANAQtSfTG5253
// While we still have characters to traverse221Please respect copyright.PENANAYPJW5hLeY6
// while the word more than one letter, do this function221Please respect copyright.PENANAmhoyXXuIv7
while (start < end) {221Please respect copyright.PENANAbVThHYZzcx
// Find the leftmost vowel221Please respect copyright.PENANAudTh2ah9l2
// while start 少於 string length() 同時 [start] 不是vowel,return start ++221Please respect copyright.PENANA2flwTHuj5w
while (start < s.length () && !isVowel(sChar[start])) {221Please respect copyright.PENANAcVfbJwbwtq
start++;221Please respect copyright.PENANAexu4VkFSMU
}221Please respect copyright.PENANAhQs5rPZdfs
// Find the rightmost vowel221Please respect copyright.PENANAVq9iA9Iieu
// while end 大於 0 同時 [end] 不是vowel,return end --221Please respect copyright.PENANAUXL2qpCj2K
while (end >= 0 && !isVowel(sChar[end])) {221Please respect copyright.PENANA2pFcguPi3o
end--;221Please respect copyright.PENANATW9ydTDRdH
}221Please respect copyright.PENANAw1HnpErdMg
// Swap them if start is left of end221Please respect copyright.PENANAyMP2RCrDjg
// swap function: (in what string, value 1, value 2), swap value 1 and 2221Please respect copyright.PENANAPNxQZRLXgi
if (start < end) {221Please respect copyright.PENANA237AyN2Oih
swap(sChar, start++, end--);221Please respect copyright.PENANAVMj77bqDU9
}221Please respect copyright.PENANAN4947XRdbg
}221Please respect copyright.PENANAhOBrd64F6f
221Please respect copyright.PENANACkMt6Qcgx4
// Converting char array back to String221Please respect copyright.PENANAqSGkllEh0N
// 顯示新的String221Please respect copyright.PENANAFXiMAD73AI
return new String(sChar);221Please respect copyright.PENANAnVR597oi6z
}221Please respect copyright.PENANAzqDvIt3Z59
};