LeetCode 题解之Goat Latin

Posted 山里的小勇子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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     }

 

以上是关于LeetCode 题解之Goat Latin的主要内容,如果未能解决你的问题,请参考以下文章

824. Goat Latin - LeetCode

LeetCode 824. Goat Latin (山羊拉丁文)

824. Goat Latin - Easy

824. Goat Latin

824. Goat Latin山羊拉丁文

LeetCode题解之Clone Graph