problem
345. Reverse Vowels of a String
class Solution { public: string reverseVowels(string s) { int left = 0, right =s.size()-1; char chl, chr; while(left<right) { if(isVowel(s[left]) &&isVowel(s[right])) { char ch = s[left]; s[left++] = s[right]; s[right--] = ch; } else if(!isVowel(s[left])) { left++; } else if(!isVowel(s[right])) { right--; } } return s; } bool isVowel(char ch) { if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') return true; else return false; } };
注意元音字母不仅有小写,还有大写的。
参考
1. Leetcode_345. Reverse Vowels of a String;
完