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.
203Please respect copyright.PENANAhX4bUQye9n
Two Pointers
class Solution {203Please respect copyright.PENANAqXXBNqUky3
// Return true if the character is a vowel (case-insensitive)203Please respect copyright.PENANAoqhZHxw7Ty
// booleann(for ture or false), 創造"isVowel"(class), 命名(char c), and add aeiouAEIOU203Please respect copyright.PENANA5R7T5ifJdI
// 當 (char c) 不是vowel,return false/ 是vowel,return true
// * a||b = ab 全为 false 时,计算结果为 false,否则为 true。203Please respect copyright.PENANAsWauDtZkmg
boolean isVowel(char c) {203Please respect copyright.PENANAcywyfmd8tw
return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'203Please respect copyright.PENANApHteYsOU8g
|| c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U';203Please respect copyright.PENANAvTFTiUzxmN
}203Please respect copyright.PENANAHGOzNZQ3U0
203Please respect copyright.PENANAvexyFvC5fM
// Function to swap characters at index x and y203Please respect copyright.PENANAzq9vdzawl7
void swap(char[] chars, int x, int y) {203Please respect copyright.PENANAoVma58VRrv
char temp = chars[x];203Please respect copyright.PENANAC2dIlblEty
chars[x] = chars[y];203Please respect copyright.PENANA7P5CnxCBAA
chars[y] = temp;203Please respect copyright.PENANA7iUwQZGE9m
}203Please respect copyright.PENANA9wSIb8gjII
203Please respect copyright.PENANAswolLjAiZ4
public String reverseVowels(String s) {203Please respect copyright.PENANAdXvbsB1MCX
// 設定最左的字母是[0]203Please respect copyright.PENANA7ftqPlJOB8
int start = 0;203Please respect copyright.PENANArZnhNYfvCe
// 設定最右的字母是[文字總長度-1].203Please respect copyright.PENANA4Sy312cpL6
int end = s.length() - 1;203Please respect copyright.PENANAzsw6OzT8tF
// 將 String 轉換為 char 數組,因為 String 在 Java 中是不可變的203Please respect copyright.PENANA0nxDfK9VLA
char[] sChar = s.toCharArray();203Please respect copyright.PENANAfK2BMPY1DX
203Please respect copyright.PENANAHbq5qblCw3
// While we still have characters to traverse203Please respect copyright.PENANA6jaNSI3UNX
// while the word more than one letter, do this function203Please respect copyright.PENANAX1DxjuwrPw
while (start < end) {203Please respect copyright.PENANAsMm4skICsQ
// Find the leftmost vowel203Please respect copyright.PENANAXr7iiSAWjj
// while start 少於 string length() 同時 [start] 不是vowel,return start ++203Please respect copyright.PENANADtnIYSeTao
while (start < s.length () && !isVowel(sChar[start])) {203Please respect copyright.PENANAq2xwVMIRwB
start++;203Please respect copyright.PENANAf9ZoyszYAR
}203Please respect copyright.PENANAkMZcI6JKsD
// Find the rightmost vowel203Please respect copyright.PENANASStCFRSdYs
// while end 大於 0 同時 [end] 不是vowel,return end --203Please respect copyright.PENANA39SCW7We9O
while (end >= 0 && !isVowel(sChar[end])) {203Please respect copyright.PENANA4UDOAoeiGa
end--;203Please respect copyright.PENANARQq4UBzmwd
}203Please respect copyright.PENANAxLQUX1B7Q0
// Swap them if start is left of end203Please respect copyright.PENANAaxyJZkgIqN
// swap function: (in what string, value 1, value 2), swap value 1 and 2203Please respect copyright.PENANAA4gl1rE8S4
if (start < end) {203Please respect copyright.PENANA5QnevK07fE
swap(sChar, start++, end--);203Please respect copyright.PENANAFsORKN5Q2o
}203Please respect copyright.PENANAdhWNGOnbXX
}203Please respect copyright.PENANAsYKeTCOTci
203Please respect copyright.PENANAfNSWV90bOL
// Converting char array back to String203Please respect copyright.PENANASirVQH4W7l
// 顯示新的String203Please respect copyright.PENANAO0BP4W8lGO
return new String(sChar);203Please respect copyright.PENANAyImRFia8Ah
}203Please respect copyright.PENANAOSi5l6rbnR
};