作者:
Rushia (みけねこ的鼻屎)
2022-11-04 10:05:10345. Reverse Vowels of a String
給你一個字串要你把他的母音字母都反轉。
Example:
Input: s = "hello"
Output: "holle"
思路:
1.寫一個判斷字母是否是母音的函數。
2.雙指標找到左邊和右邊的第一個母音。
3.把兩個母音交換之後繼續往字串裡面緊縮直到 i == j。
JavaCode:
class Solution {
public String reverseVowels(String s) {
int n = s.length();
char[] chars = s.toCharArray();
int i = 0, j = n - 1;
while (i < j) {
while (i < j && !isVowel(chars[i])) i++;
while (i < j && !isVowel(chars[j])) j