345 Reverse Vowels of a String 反轉字符串中的元音字母
阿新 • • 發佈:2018-04-15
class ble otc -s else 字符串 反轉字符串 一個 -a
編寫一個函數,以字符串作為輸入,反轉該字符串中的元音字母。
示例 1:
給定 s = "hello", 返回 "holle".
示例 2:
給定 s = "leetcode", 返回 "leotcede".
註意:
元音字母不包括 "y".
詳見:https://leetcode.com/problems/reverse-vowels-of-a-string/description/
C++:
class Solution { public: string reverseVowels(string s) { int left = 0, right= s.size() - 1; while (left < right) { if (isVowel(s[left]) && isVowel(s[right])) { swap(s[left++], s[right--]); } else if (isVowel(s[left])) { --right; } else { ++left; } } return s; } bool isVowel(char c) { return c == ‘a‘ || c == ‘e‘ || c == ‘i‘ || c == ‘o‘ || c == ‘u‘ || c == ‘A‘ || c == ‘E‘ || c == ‘I‘ || c == ‘O‘ || c == ‘U‘; } };
參考:https://www.cnblogs.com/grandyang/p/5426682.html
345 Reverse Vowels of a String 反轉字符串中的元音字母