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 ...
随机推荐
- EXP-00056: ORACLE error 12154 encountered
今天用EXP命令导Oracle数据库,出现如下错误信息: [oracle@yingxiang-testServer1 ~]$ exp imgpf/imgpf@orcl file=/prlife/db/ ...
- cocos2d-x 重力感应
本文没你想象的那么,,复杂.事实上就是通过重力感应控制个小球移动而已. 先看头文件: #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE ...
- 【GMT43液晶显示模块】发布原理图、出厂代码
GMT43是一款内置4.3寸真彩液晶显示模块,其内置高速ARM Cortex-M4处 理器,主频高达180MHz,并包含丰富的外设接口. GMT43拥有丰富的资源,包含RS-232,RS-485,RS ...
- 【emWin】例程二十九:窗口对象——Messagebox
简介: 使用MESSAGEBOX 小工具可在带有标题栏和“确定”按钮(必须按下才能关闭窗口)的 框架窗口中显示消息.本实验通过点击下图中的按键来创建一个Messagebox对话框. 触摸校准(上电可选 ...
- Java编程的逻辑 (86) - 动态代理
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- .net core实现跨域
什么是跨域在前面已经讲解过了,这里便不再讲解,直接上代码. 一.后台API接口 用.net core创建一个Web API项目负责给前端界面提供数据. 二.前端界面 建立两个MVC项目,模拟不同的ip ...
- Leaflet API翻译
转自: http://jsrookie.iteye.com/blog/2318972(上) http://jsrookie.iteye.com/blog/2318973(下) L.Map API各种类 ...
- IntellIJ IDEA 配置 Vue 支持 打开Vue项目
[参考]零基础 Vue 开发环境搭建 打开运行Vue项目 IDEA版本: IntelliJ IDEA 2017.2 Windows 7 X64 IntelliJ IDEA下载地址:https://ww ...
- [Converge] Batch Normalisation
参考:https://www.zhihu.com/question/38102762 参考:CNN和RNN中如何引入BatchNorm 论文:Batch Normalization: Accelera ...
- Git 学习笔记--拉取远程分支到本地
1.查看远程分支,和上面的第一步相同2. 从远程获取最新版本到本地 git fetch origin master:temp git fetch origin master:temp 这句命令的意思是 ...