【LeetCode】345. 文字列の母音文字を反転
5748 ワード
タイトルリンク:https://leetcode-cn.com/problems/reverse-vowels-of-a-string/description/
タイトルの説明
文字列を入力として関数を作成し、文字列のアクセント文字を反転します.
例
入力:hello出力:holle
入力:leetcode出力:leotcede
解決策
テーマは簡単だ
タイトルの説明
文字列を入力として関数を作成し、文字列のアクセント文字を反転します.
例
入力:hello出力:holle
入力:leetcode出力:leotcede
解決策
テーマは簡単だ
class Solution {
public:
string reverseVowels(string s) {
int left=0,right=s.size()-1;
map<char,int> map1;
map1['a']=1;map1['e']=1;map1['i']=1;map1['o']=1;map1['u']=1;
map1['A']=1;map1['E']=1;map1['I']=1;map1['O']=1;map1['U']=1;
while(left<right){
while (!map1[s[left]]) left++;
while (!map1[s[right]]) right--;
if (left<right) swap(s[left],s[right]);
left++;right--;
}
return s;
}
};