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 ...
随机推荐
- Java基础知识常识总结
Java基础知识常识总结 1.面向对象的特征以及对他的理解? 封装,继承,多态,如果再增加一条就是抽象. 2.Object有几种方法,分别是什么? 在JDK1.5中的11种方法,分别有: toStri ...
- jQuery autocomplete -默认
<!doctype html> <html> <head> <meta name="content-type" content=" ...
- 处理事件冒泡,阻止默认事件工具类,兼容IE
//处理事件冒泡,阻止默认事件工具类,兼容IEvar eventUtil={ // 添加句柄 addHandler:function(element,type,handler){ if(element ...
- Android代码速查,写给新手的朋友们[转]
原文地址:http://www.open-open.com/lib/view/open1397286499090.html 0 android 创建按钮 Button button = new But ...
- 轻松搭建持续集成工具jenkins
1.Jenkins介绍1)什么是持续集成随着软件开发复杂度的不断提高,团队开发成员间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.尤其是近些年来,敏捷(Agile) 在软 ...
- JS中立即执行函数的理解
1.匿名函数不能单独定义,必须进行赋值操作或者立即执行,否则会被JS引擎定义为语法错误 function(){alert(dada);} VM229:1 Uncaught SyntaxError: U ...
- Smarty的循环
(1)section.sectionelse功能多,参数多.或许不是太实用.是smarty用来做循环操作的函数之一.(2)了解基本属性name和loop <section name=articl ...
- 【转载】Jedis对管道、事务以及Watch的操作详细解析
转载地址:http://blog.csdn.net/liyantianmin/article/details/51613772 1.Pipeline 利用pipeline的方式从client打包多条命 ...
- Linux软件包的管理
RPM软件包 RPM软件包一般的安装位置(分散): 管理程序:/sbin/* /usr/sbin/* 普通用户程序:/bin/* /usr/bin/* rpm -q ===> rpm -quer ...
- HttpUploader2-queue版本
1.1 2016版本 目标: 1.大幅度优化JS代码,面向开发人员更加友好,逻辑清晰,代码简洁,便于阅读,定制开发,扩展,更加符合企业级应用需求. 2.大幅度使用并发技术来提高上传速度,与HttpUp ...