zoukankan      html  css  js  c++  java
  • 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     }
    pp
  • 相关阅读:
    大道至简第四章读后感
    进度条08
    大道至简第五章读后感
    加密算法
    程序从命令行接收多个数字,求和之后输出结果。
    用JAVA制作简单登录窗口
    进度条07
    冲刺07
    冲刺06
    冲刺05
  • 原文地址:https://www.cnblogs.com/wangxiaoyong/p/9329331.html
Copyright © 2011-2022 走看看