345. Reverse Vowels of a String(C++)
时间:2022-03-26 06:28
345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string.
Example 1:
Given s = "hello", return "holle".
Example 2:
Given s = "leetcode", return "leotcede".
题目大意:
编写函数输入一个字符串,将其中的元音字母逆置。
解题方法:
注意事项:
C++代码:
class Solution { public: string reverseVowels(string s) { int left=0; int right=s.size()-1; string t="aeiouAEIOU"; while(left<right) { if(t.find(s[left])==string::npos) { ++left; } else if(t.find(s[right])==string::npos) { --right; } else { swap(s[left++],s[right--]); } } return s; } };