解题思路:
设置两个指针 left 和 right,大循环中的每一个循环,找到前面、后面两个元音字母,然后交换这两个元音。
class Solution {
public:
bool whethertrue(char& c){
if(c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U') return true;
return false;
}
string reverseVowels(string s) {
if(s.empty()) return s;
int n = s.length();
int left = 0;
int right = n - 1;
while(left < right){
while(left < n &&!whethertrue(s[left])){
left++;
}
while(right > 0 && !whethertrue(s[right])){
right--;
}
if(left < right){
char tmp = s[left];
s[left] = s[right];
s[right] = tmp;
}
left++;
right--;
}
return s;
}
};
本文介绍了一种在字符串中反转元音字母位置的算法。通过使用双指针技术,算法在大循环中寻找字符串前后两端的元音字母,并进行交换。此方法提高了效率,避免了不必要的字符移动。
491

被折叠的 条评论
为什么被折叠?



