您的位置:首页 > 其它

leetcode 345Reverse vowels of a string

2016-04-25 19:44 375 查看
元音字母有大小写

整体思路,顺序度string,碰到元音字母就存起来,读完再顺序读一遍,这次碰到元音字母就用存起来的数组倒序替换

class Solution {

public:

string reverseVowels(string s) {

istringstream str(s);//将string字符绑定到输入流

vector<char>vowel;

char ch;

while(str>>ch){

if(isVowel(ch))

vowel.push_back(ch);//如果是元音字母就保存

}

for(int i = 0,j = 0;i != s.size();++i)

if(isVowel(s[i])){

s[i] = vowel[vowel.size() - 1 - j];

j += 1;

}

return s;

}

//判断是否为元音字母,含大小写

bool isVowel(char ch)

{

int flag = 0;

switch(ch){

case 'a':

case 'A':

case 'e':

case 'E':

case 'i':

case 'I':

case 'o':

case 'O':

case 'u':

case 'U':

flag = 1;

break;

default:

flag = 0;

break;

}

if(flag)

return 1;

else return 0;

}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: