
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.
291Please respect copyright.PENANAuuUndwj4IV
Two Pointers
class Solution {291Please respect copyright.PENANACHuuEaY5uG
// Return true if the character is a vowel (case-insensitive)291Please respect copyright.PENANAiSE2etaKhM
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU291Please respect copyright.PENANA4o6z42vTIF
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。291Please respect copyright.PENANAxg1s7TBbTk
boolean isVowel(char c) {291Please respect copyright.PENANAJiodeiOVfz
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'291Please respect copyright.PENANAGaUGNU6vsw
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';291Please respect copyright.PENANAssMowzPr0r
}291Please respect copyright.PENANA3UWSJcj2yP
291Please respect copyright.PENANAQO80JxEnDn
// Function to swap characters at index x and y291Please respect copyright.PENANAZ8vWBuRq5y
void swap(char[] chars, int x, int y) {291Please respect copyright.PENANAzrEMJhWUsE
char temp = chars[x];291Please respect copyright.PENANAuREiY5yH8x
chars[x] = chars[y];291Please respect copyright.PENANAiF70isqsag
chars[y] = temp;291Please respect copyright.PENANAbNJIgeriBc
}291Please respect copyright.PENANAEuxq7KoKL7
291Please respect copyright.PENANABhBrKalHPk
public String reverseVowels(String s) {291Please respect copyright.PENANAvc7qwvejZ2
// 設定最左的字母是[0]291Please respect copyright.PENANArxPPhyfifk
int start = 0;291Please respect copyright.PENANApqNspByBbd
// 設定最右的字母是[文字總長度-1].291Please respect copyright.PENANAyV9EUbqLvf
int end = s.length() - 1;291Please respect copyright.PENANAJkV50vJ96q
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的291Please respect copyright.PENANAt0qLTpPkb6
char[] sChar = s.toCharArray();291Please respect copyright.PENANAwp4mUVe63W
291Please respect copyright.PENANAF7YzZGDq7x
// While we still have characters to traverse291Please respect copyright.PENANAMM4vKNfQbJ
// while the word more than one letter, do this function291Please respect copyright.PENANAYU31MPsfHL
while (start < end) {291Please respect copyright.PENANAxcjtEYrPaj
// Find the leftmost vowel291Please respect copyright.PENANAAfeEQHEnXA
// while start 少於 string length() 同時 [start] 不是vowel,return start ++291Please respect copyright.PENANAMFS3tnFwAx
while (start < s.length () && !isVowel(sChar[start])) {291Please respect copyright.PENANAf2XmjArDrm
start++;291Please respect copyright.PENANAM8Ff0WalRL
}291Please respect copyright.PENANAPsMATfzRPs
// Find the rightmost vowel291Please respect copyright.PENANAa7BcHX4dXk
// while end 大於 0 同時 [end] 不是vowel,return end --291Please respect copyright.PENANAyggnI9nS5t
while (end >= 0 && !isVowel(sChar[end])) {291Please respect copyright.PENANAotVKDUVG6S
end--;291Please respect copyright.PENANAAJD83bL2pz
}291Please respect copyright.PENANAVyU8hElwHf
// Swap them if start is left of end291Please respect copyright.PENANAf4Mm7pAVc9
// swap function: (in what string, value 1, value 2), swap value 1 and 2291Please respect copyright.PENANAWtMTApCYBV
if (start < end) {291Please respect copyright.PENANAacyH8RH5e1
swap(sChar, start++, end--);291Please respect copyright.PENANAhqi6A4AuWn
}291Please respect copyright.PENANAQgY0ziyne8
}291Please respect copyright.PENANAMdpKB9caaa
291Please respect copyright.PENANAWrE20HeDLq
// Converting char array back to String291Please respect copyright.PENANAlFrLdBUJNc
// 顯示新的String291Please respect copyright.PENANASrl3KXpFLl
return new String(sChar);291Please respect copyright.PENANAI4hdbSYcb9
}291Please respect copyright.PENANAD2ZW5lz6lE
};