leetCode 345. Reverse Vowels of a String 字符串
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".
Note:
The vowels does not include the letter "y".
思路:
找到元音字母,标记位置,进行置换。
代码如下:
//vowels(元音字母)包括:a,e,i,o,u.classSolution{public:boolisVowels(charc){charvowels[10]={'a','e','i','o','u','A','E','I','O','U'};for(inti=0;i<10;i++){if(c==vowels[i]){returntrue;break;//return和break在一起都有反应么?}}returnfalse;}stringreverseVowels(strings){vector<int>recordIndex;vector<char>str;for(inti=0;i<s.size();i++){str.push_back(s.at(i));}for(inti=0;i<str.size();i++){if(isVowels(str[i])){recordIndex.push_back(i);}}for(inti=0;i<recordIndex.size()/2;i++){charleft=str[recordIndex[i]];charright=str[recordIndex[recordIndex.size()-1-i]];str[recordIndex[i]]=right;str[recordIndex[recordIndex.size()-1-i]]=left;}s="";for(inti=0;i<str.size();i++){s.append(1,str[i]);}returns;}};
2016-08-08 10:52:10
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。