LeetCode 题解之Goat Latin

1、问题描述

2、问题分析

将句子拆分成单词,然后放入vector中,对每一个单词做改变,最后连成句子返回。

3、代码

 1 string toGoatLatin(string S) {
 2         vector<string> words;
 3         for(string::iterator it = S.begin() ; it != S.end(); ++it ){
 4             string::iterator it_i = it ;
 5             while( it_i != S.end() && isalpha( *it_i) ){
 6                 ++it_i ;
 7             }
 8             string word(it,it_i);
 9             words.push_back( word );
10             if( it_i != S.end() ){
11                 it = it_i;
12             }else{
13                 it = it_i -1 ;
14             }
15         }
16         
17         string result ;
18         string a = "a";
19         set<string> vowels{"a","e","i","o","u","A","E","I","O","U"};
20         for( auto &s : words ){
21             if( vowels.find( s.substr(0,1) ) != vowels.end() ){
22                 s += "ma";
23             }else{
24                 s += s[0];
25                 s.erase( s.begin() );
26                 s += "ma";
27             }
28             s += a;
29             a += "a";
30             result += s += " ";
31         }
32         
33         result.erase( result.end() - 1);
34         return result ;
35         
36         
37     }

 

posted @ 2018-07-18 15:17  山里的小勇子  阅读(220)  评论(0编辑  收藏  举报