
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.
331Please respect copyright.PENANAfrcUUWDA4B
Two Pointers
class Solution {331Please respect copyright.PENANAM5xyr5li3c
// Return true if the character is a vowel (case-insensitive)331Please respect copyright.PENANAOQd45xwlA7
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU331Please respect copyright.PENANA73GQHXNy9Y
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。331Please respect copyright.PENANAfiJgkZXlm7
boolean isVowel(char c) {331Please respect copyright.PENANASxE73RghvA
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'331Please respect copyright.PENANAR20SBW7kVx
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';331Please respect copyright.PENANApx7YBAofUt
}331Please respect copyright.PENANADP45snvDN1
331Please respect copyright.PENANALtbGsiExRJ
// Function to swap characters at index x and y331Please respect copyright.PENANAW0c9ht6QFW
void swap(char[] chars, int x, int y) {331Please respect copyright.PENANA2RFFE14oPQ
char temp = chars[x];331Please respect copyright.PENANAwG6vDT7olV
chars[x] = chars[y];331Please respect copyright.PENANAOPkWxinmSp
chars[y] = temp;331Please respect copyright.PENANAicpEAMU8w4
}331Please respect copyright.PENANAMzoCh1iS0X
331Please respect copyright.PENANA4ZhRbfF3nR
public String reverseVowels(String s) {331Please respect copyright.PENANAh1g222TOms
// 設定最左的字母是[0]331Please respect copyright.PENANADw0fSWpnfl
int start = 0;331Please respect copyright.PENANAVvwtjNVpKY
// 設定最右的字母是[文字總長度-1].331Please respect copyright.PENANAd9ayCDlYwb
int end = s.length() - 1;331Please respect copyright.PENANA8v4j1guYp5
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的331Please respect copyright.PENANAnLtpUW2p04
char[] sChar = s.toCharArray();331Please respect copyright.PENANAbLBNUx0Ag1
331Please respect copyright.PENANAhWPHfJQVwm
// While we still have characters to traverse331Please respect copyright.PENANAXxIQ92yIav
// while the word more than one letter, do this function331Please respect copyright.PENANAatxP3EC4X2
while (start < end) {331Please respect copyright.PENANATRMJiYFNQ0
// Find the leftmost vowel331Please respect copyright.PENANARy0P6Jxrox
// while start 少於 string length() 同時 [start] 不是vowel,return start ++331Please respect copyright.PENANAcX87z1nDRi
while (start < s.length () && !isVowel(sChar[start])) {331Please respect copyright.PENANArLZ6Mjpbd5
start++;331Please respect copyright.PENANAaC9Loa10yV
}331Please respect copyright.PENANA0H8F7Jd5od
// Find the rightmost vowel331Please respect copyright.PENANATK3gjoFHrl
// while end 大於 0 同時 [end] 不是vowel,return end --331Please respect copyright.PENANANmm7eKR8fi
while (end >= 0 && !isVowel(sChar[end])) {331Please respect copyright.PENANAbs5vn6HwXi
end--;331Please respect copyright.PENANATHzfuAJ8fa
}331Please respect copyright.PENANAEszoReVrYI
// Swap them if start is left of end331Please respect copyright.PENANAimKl5tkaaZ
// swap function: (in what string, value 1, value 2), swap value 1 and 2331Please respect copyright.PENANASVU3JV6wK8
if (start < end) {331Please respect copyright.PENANAILF8HQvBam
swap(sChar, start++, end--);331Please respect copyright.PENANAS5VfincvG2
}331Please respect copyright.PENANAYrIvMJEmL6
}331Please respect copyright.PENANALxavZgebTa
331Please respect copyright.PENANAyNiDZAWUXJ
// Converting char array back to String331Please respect copyright.PENANAtwBWaWR571
// 顯示新的String331Please respect copyright.PENANAPjRNnXYaDj
return new String(sChar);331Please respect copyright.PENANAluIdvXkeLl
}331Please respect copyright.PENANAtZDL10WW3s
};