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.
题目分析及思路
给定一个句子,其中只包含word和space,word只由大小写字母组成。先将这个句子进行转换,转换规则如下:
1)若一个word以元音字母开头,则在这个word末尾添加'ma'
2)若一个word以辅音字母开头,则将这个word开头字母移到末尾,并在这个word末尾添加'ma'
3)对句中的每一个word,根据它的索引在word末尾添加'a'。若索引为0,则添加一个'a',为1则添加两个'a',以此类推
最后将转换好的句子返回。
python代码
class Solution:
def toGoatLatin(self, S: str) -> str:
S = S.split()
s_gls = ''
for idx, s in enumerate(S):
if s[0] in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']:
s_gl = s + 'ma'
else:
s_gl = s[1:] + s[0] + 'ma'
s_gl += (idx+1)*'a'
s_gls += s_gl + ' '
return s_gls.strip()
LeetCode 824 Goat Latin 解题报告的更多相关文章
- 【LeetCode】824. Goat Latin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【Leetcode_easy】824. Goat Latin
problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- 【emWin】例程三十一:窗口对象——Multipage
简介: ULTIPAGE 类似于笔记本中的分隔卡或文件柜中的标签.通过使用MULTIPAGE 小工具,应用程序可为窗口或对话框的相同区域定义多个页面.每个页面包含特定 类型的信息或用户选择相应页面时应 ...
- Jacky扯淡系列 – 验证码
1 验证码的用途 防止恶意用户的csrf,比如一些bot的重复请求,类似的有密码破解等操作. 但是验证码这个东西会降低用户的体验度,因此不能将其作为必备的防护措施. 2 常见的验证码形式 通常的验证码 ...
- Java知多少(103)网络编程之IP地址和InetAddress类
Java语言的优势之一是Java程序能访问网络资源.Java提供一系列的类支持Java程序访问网络资源. TCP/IP协议和IP地址 为了进行网络通信,通信双方必须遵守通信协议.目前最广泛使用的是TC ...
- postman中 form-data、x-www-form-urlencoded、raw、binary的区别--转
原文地址:http://blog.csdn.net/ye1992/article/details/49998511 1.form-data: 就是http请求中的multipart/form-dat ...
- 安卓开发笔记——个性化TextView(新浪微博)
这几天在仿写新浪微博客户端,在处理微博信息的时候需要处理关键字高亮和微博表情,查了一些资料,决定记录点东西 先来看下效果图: 像以上这种#话题#,@XXX昵称,HTTP:网页链接等元素,在微博里是被高 ...
- json_decode 为空
传递参数 wsk/addorder?goods=[{"gsn":802006,"number":1},{"gsn":103761," ...
- python中用修饰器进行异常日志记录
当脚本中需要进行的的相同的异常操作很多的时候,可以用修饰器来简化代码.比如我需要记录抛出的异常: 在log_exception.py文件中, import functools import loggi ...
- 【hadoop】 hdfs shell 命令交互
1.put 本地文件上传至hdfs中 2. cat 查看内容 3. 删除文件,文件夹 4. ls 5. copyFromLocal 复制本地文件到HDFS , copyToLocal hdfs 复制到 ...
- postmain 通过函数动态设置参数
调用服务器的服务,其中有个参数是签名,签名需要计算,需要写一个本地函数. 下一步 pre-request Script 的代码如下: (function($) { if(!$.encoding) $. ...
- day_5.24py
世间万物皆对象! 闭包就是内部函数中对enclosing作用域的变量进行引用. 装饰器 最开始就说,这篇博客始于闭包,终于闭包,所以装饰器不多说,只说四句话:1.装饰器就是对闭包的使用:2.装饰器用来 ...