
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.
348Please respect copyright.PENANABsoO4JEyJl
Two Pointers
class Solution {348Please respect copyright.PENANAAAlnxty9Jd
// Return true if the character is a vowel (case-insensitive)348Please respect copyright.PENANAMMQbmjq3Fk
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU348Please respect copyright.PENANA9MzcfU1NZs
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。348Please respect copyright.PENANAaLVXL27RC0
boolean isVowel(char c) {348Please respect copyright.PENANAGcV8mqf8yc
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'348Please respect copyright.PENANAWPJCQaF468
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';348Please respect copyright.PENANA7eXXOjVQSK
}348Please respect copyright.PENANAk7oAkWfGTU
348Please respect copyright.PENANA2EahRTGueW
// Function to swap characters at index x and y348Please respect copyright.PENANAMkhjf9sjXc
void swap(char[] chars, int x, int y) {348Please respect copyright.PENANA0aitxKRpLk
char temp = chars[x];348Please respect copyright.PENANA8zJghQR3Re
chars[x] = chars[y];348Please respect copyright.PENANAQOoWjewdMp
chars[y] = temp;348Please respect copyright.PENANAxoS0JKX6wO
}348Please respect copyright.PENANAHqWLlu8dfS
348Please respect copyright.PENANAlxWLyldzCQ
public String reverseVowels(String s) {348Please respect copyright.PENANAhp5p3fMNdZ
// 設定最左的字母是[0]348Please respect copyright.PENANATrmudfc7B0
int start = 0;348Please respect copyright.PENANAlyllSe40qD
// 設定最右的字母是[文字總長度-1].348Please respect copyright.PENANAytssBmPyxx
int end = s.length() - 1;348Please respect copyright.PENANAqVfuSPGmA5
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的348Please respect copyright.PENANA2Y0dLaZBwQ
char[] sChar = s.toCharArray();348Please respect copyright.PENANAhi3lcz83j3
348Please respect copyright.PENANAXK17v9lihF
// While we still have characters to traverse348Please respect copyright.PENANAGHJLyMmPj3
// while the word more than one letter, do this function348Please respect copyright.PENANAcdIKHpesmO
while (start < end) {348Please respect copyright.PENANA1CZeJ3Pkgg
// Find the leftmost vowel348Please respect copyright.PENANAnUMcVIxwA9
// while start 少於 string length() 同時 [start] 不是vowel,return start ++348Please respect copyright.PENANARKmas3JT5V
while (start < s.length () && !isVowel(sChar[start])) {348Please respect copyright.PENANAc0cGZkqvMw
start++;348Please respect copyright.PENANAeMjGKGmfBc
}348Please respect copyright.PENANAL3IlhSlZCn
// Find the rightmost vowel348Please respect copyright.PENANAbATY81n1iw
// while end 大於 0 同時 [end] 不是vowel,return end --348Please respect copyright.PENANArPRwGMlCxo
while (end >= 0 && !isVowel(sChar[end])) {348Please respect copyright.PENANA8Au8NmQgFk
end--;348Please respect copyright.PENANAsnEmCrEl4E
}348Please respect copyright.PENANA6Uw9RiNXSe
// Swap them if start is left of end348Please respect copyright.PENANAClucg41soY
// swap function: (in what string, value 1, value 2), swap value 1 and 2348Please respect copyright.PENANAE0tj0bcXLI
if (start < end) {348Please respect copyright.PENANAIoowKvEFLa
swap(sChar, start++, end--);348Please respect copyright.PENANAVolPx1uKur
}348Please respect copyright.PENANAT5z6DoXOMc
}348Please respect copyright.PENANARNLQFYx0yC
348Please respect copyright.PENANAEsLSN2qeja
// Converting char array back to String348Please respect copyright.PENANAxPk0LFuNlX
// 顯示新的String348Please respect copyright.PENANACt7HET7Xgh
return new String(sChar);348Please respect copyright.PENANA44YamNeJt3
}348Please respect copyright.PENANAU40aji8v7e
};