mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
Create 345. Reverse Vowels of a String.md
This commit is contained in:
parent
708438f314
commit
898a72d04c
24
345. Reverse Vowels of a String.md
Normal file
24
345. Reverse Vowels of a String.md
Normal file
@ -0,0 +1,24 @@
|
||||
# [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++
|
||||
```
|
||||
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;
|
||||
}
|
||||
};
|
||||
```
|
Loading…
Reference in New Issue
Block a user