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

题目标签:String

 首先把vowel letters 保存入 HashSet。

然后把S 拆分成 各个 word,遍历每一个 word:

   当 word 第一个 字母不是 vowel 的时候,把第一个char 加到最后;

   然后添加“ma” 和 “a“ 到最后;

   添加新的"a";

   把新的 word 加入 result,还要记得加入空格。

Java Solution:

Runtime beats 62.66%

完成日期:10/12/2018

关键词:String

关键点:利用HashSet保存vowel

 1 class Solution
 2 {
 3     public String toGoatLatin(String S)
 4     {
 5         String result = "";
 6         Set<Character> vowelSet = new HashSet<>();
 7         String addOn = "a";
 8
 9         for (char c: new char[]{‘a‘, ‘e‘, ‘i‘, ‘o‘, ‘u‘, ‘A‘, ‘E‘, ‘I‘, ‘O‘, ‘U‘})
10             vowelSet.add(c);
11
12         for(String word : S.split(" "))
13         {
14             if(result.length() > 0)
15                 result += " ";
16
17             if(!vowelSet.contains(word.charAt(0)))
18             {
19                 word = word.substring(1) + word.charAt(0);
20             }
21
22             word += "ma" + addOn;
23             addOn += "a";
24
25             result += word;
26         }
27
28         return result;
29     }
30 }

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/9784936.html

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

LeetCode 824. Goat Latin (山羊拉丁文)的相关文章

824. Goat Latin山羊拉丁文

[抄题]: 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 La

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

824. Goat Latin

1 class Solution 2 { 3 public: 4 string toGoatLatin(string S) 5 { 6 S.push_back(' '); //add a space that the loop can deal with the last word 7 string a(150,'a'); // string to add 'a' 8 unordered_set<char> vowel={'a','e','i','o','u','A','E','I','O',

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() && is

Leetcode简单题61~80

61.思路:bin# 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量).# 示例 1:# 输入:00000000000000000000000000001011# 输出:3# 解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'.# 示例 2:# 输入:00000000000000000000000010000000# 输出:1# 解释:输入的二进制串 0000000000000000

【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranking: 440/2797.这次题目似乎比较简单,因为我比赛的时候前三题全做出来了(1:12:39),然后第四题有思路,正在写,没写完,比赛完了写完提交也对了. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [821]

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

*LeetCode--Goat Latin

Goat Latin 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 Go

LeetCode-824 划水记录3

给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin 的虚构语言). 山羊拉丁文的规则如下: 如果单词以元音开头(a, e, i, o, u),在单词后添加"ma".例如,单词"apple"变为"applema". 如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma".