leetcode-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 Latin are as follows:
- If a word begins with a vowel (a, e, i, o, or u), append
"ma"to the end of the word.
For example, the word 'apple' becomes 'applema'. - If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add
"ma".
For example, the word"goat"becomes"oatgma". - Add one letter
'a'to the end of each word per its word index in the sentence, starting with 1.
For example, the first word gets"a"added to the end, the second word gets"aa"added to the end and so on.
Return the final sentence representing the conversion from S to Goat Latin.
Example 1:
Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Example 2:
Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
Notes:
Scontains only uppercase, lowercase and spaces. Exactly one space between each word.1 <= S.length <= 150.
要完成的函数:
string toGoatLatin(string S)
说明:
1、这道题给定一个字符串S,里面包含单词,大小写敏感,单词之间以空格隔开,要求把英文转化为“goat latin”,规则如下:
如果单词以元音字母a/e/i/o/u以及它们的大写形式开头,那么在单词的最后面加“ma”。
如果单词不以元音字母开头,那么把单词的首字母放到最后面,再在单词的最后面加“ma”。
第一个单词在最后再加“a”,第二个单词在最后再加“aa”,第三个单词在最后再加“aaa”,依此类推。
2、题意清晰,这道题目很容易。
直接分享给大家代码(附详解),如下:
string toGoatLatin(string S)
{
int i=0,s1=S.size(),j=1,count=0;//i表示单词首字母位置,j表示空格位置
set<char>set1{'a','e','i','o','u','A','E','I','O','U'};
string word;//代表取出的每个单词
string res="";
while(i<s1)
{
if(S[j]==' '||S[j]=='\0')//如果碰到空格或者结束符号
{
count++;
word=S.substr(i,j-i);//取出单词,子字符串
if(set1.count(word[0])==0)//首字母非元音
{
word=word.substr(1,word.size()-1)+word[0]+"ma";
for(int k=0;k<count;k++)
word=word+'a';
}
else//首字母为元音字母
{
word=word+"ma";
for(int k=0;k<count;k++)
word=word+'a';
}
res=res+word+' ';//每个单词存储在字符串中,添加一个空格位
i=j+1;//更新i的位置
j=i+1;//更新j的位置
}
else
j++;
}
res=res.substr(0,res.size()-1);//去掉最后一次添加的空格位
return res;
}
上述代码逻辑清晰,实测6ms,因为服务器接受到的cpp submissions有限,所以没有打败的百分比。
leetcode-824-Goat Latin(字符串的处理)的更多相关文章
- LeetCode 824 Goat Latin 解题报告
题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...
- [LeetCode] 824. Goat Latin
Description A sentence S is given, composed of words separated by spaces. Each word consists of lowe ...
- LeetCode 824. Goat Latin (山羊拉丁文)
题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...
- 824. Goat Latin - LeetCode
Questioin 824. Goat Latin Solution 题目大意:根据要求翻译句子 思路:转换成单词数组,遍历数组,根据要求转换单词 Java实现: 用Java8的流实现,效率太低 pu ...
- 【Leetcode_easy】824. Goat Latin
problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...
- 【LeetCode】824. Goat Latin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode&Python] Problem 824. Goat Latin
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- 824. Goat Latin山羊拉丁文
[抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...
- [LeetCode] 824. Goat Latin_Easy
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- 824. Goat Latin
class Solution { public: string toGoatLatin(string S) { S.push_back(' '); //add a space that the loo ...
随机推荐
- 无需写try/catch,也能正常处理异常
对于企业应用的开发者来说,异常处理是一件既简单又复杂的事情.说其简单,是因为相关的编程无外乎try/catch/finally+throw而已:说其复杂,是因为我们往往很难按照我们真正需要的策略来处理 ...
- arping
强制交换机刷新MAC arping -I em2(网卡名称) 58.215.88.8(Vip)
- ubuntu14.04下安装qt5
1.sudo apt-get install build-essential 2.先打开终端快捷键ctrl+t 3. 然后输入: sudo apt-get install cmake qt5-defa ...
- Web测试项目计划与安排
本次Web测试项目实践的需求如下: 1 选中某一款产品(暂且选择博客园和CSDN进行横向比较),对被测产品进行评测: 2 进行用户调研: 3 对产品进行定量的评价: 4 对这个产品进行分析: 5 例会 ...
- 转:开启命令行下的社交-webqq脚本
最近一直在命令行下工作,除了 Google Chrome,几乎很少接触 GUI 相关的软件.前段时间把手机上的 QQ 给卸载了,希望可以把时间凝聚在更加有价值的位置,今天突然又想起了这个软件,突发奇想 ...
- ceph常用指令
一.集群 1.启动一个ceph 进程 启动mon进程 service ceph start mon.node1 启动msd进程 service ceph start mds.node1 启动osd进 ...
- 优秀前端工程师必备: (总结) 清除原生ios按钮样式
写移动端的web开发时, 需要清除IOS本身的各种样式: 1.消除ios按钮原生样式, 给按钮加自定义样式: input[type="button"], input[type=&q ...
- PatternLayoutEncoder 输出格式
ch.qos.logback.classic.encoder.PatternLayoutEncoder ch.qos.logback.classic.PatternLayout ch.qos.logb ...
- FileAppender
http://logback.qos.ch/manual/appenders.html#FileAppender <configuration> <appender name=&qu ...
- 个人写spark小测试
写脚本生成类似文件 java 代码 封装类 package day0327; import java.util.UUID; public class data { private String ip; ...