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     }

原文地址:https://www.cnblogs.com/wangxiaoyong/p/9329331.html

时间: 2024-10-09 06:19:12

LeetCode 题解之Goat Latin的相关文章

824. Goat Latin - Easy

A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.) The rules of Goat Latin ar

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

[LeetCode 题解]: Reverse Nodes in K-Groups

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

[LeetCode 题解]: Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 题意: 给定一个链表,找到环起始的位置.如果环不存在,返回NULL. 分析: (1)首先要判断该链表是否有环.如果没有环,那么返回NULL. (2)其次,当已知环存在后,寻找环起始的位置. 思路: (