LeetCode/solutions/345. Reverse Vowels of a String.md
2019-09-13 23:08:41 +08:00

25 lines
759 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [345. Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/description/)
# 思路
翻转字符串中的元音字母即A、E、I、O、U、a、e、i、o、u。常规题
# C++
```C++
class Solution {
private:
bool isVowel(char c){
if('A' <= c && c <= 'Z') c = c - 'A' + 'a';
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true;
return false;
}
public:
string reverseVowels(string s) {
int low = 0, high = s.size() - 1;
while(low < high){
while(low < high && !isVowel(s[low])) low++;
while(low < high && !isVowel(s[high])) high--;
if(low < high) swap(s[low++], s[high--]);
}
return s;
}
};
```