leetcode 345. Reverse Vowels of a String
string reverseVowels(string s) { for (int i = 0, j = s.size() - 1; i < j;) { bool l = isVowels(s[i]); bool r = isVowels(s[j]); if (l && r) swap(s[i++], s[j--]); else { if (!l) i++; if (!r) j--; } } return move(s); } bool isVowels(const char& c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'; }
【本文章出自博客园willaty,转载请注明作者出处,误差欢迎指出~】