From 898a72d04c21d2710eb7c9d0464fa3a1c25766a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E6=A0=91=E6=A3=AE?= <14021051@buaa.edu.cn> Date: Thu, 4 Oct 2018 23:37:29 +0800 Subject: [PATCH] Create 345. Reverse Vowels of a String.md --- 345. Reverse Vowels of a String.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 345. Reverse Vowels of a String.md diff --git a/345. Reverse Vowels of a String.md b/345. Reverse Vowels of a String.md new file mode 100644 index 0000000..2a25e32 --- /dev/null +++ b/345. Reverse Vowels of a String.md @@ -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; + } +}; +```