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 ...
随机推荐
- vue环境配置脚手架环境搭建vue工程目录
首先在初始化一个vue项目之前,我们需要下载node.js,并且安装! 下载地址: nodejs.cn/download 安装完成之后,windows+r 运行命令 cmd 输入node -v 检 ...
- Python访问MongoDB,并且转换成Dataframe
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/7/13 11:10 # @Author : baoshan # @Site ...
- php5.6-lumen与php5.6-phalcon性能对比
场景一: 两个框架简单输出helloworld phalcon:{QPS:7349,平均响应时间:124.11ms},lumen{QPS:1327,平均响应时间:721.54ms} QPS: phal ...
- Python3数字(Number)
一.数学函数 二.随机数函数 三.三角函数 四.数学常量
- idea java 非web程序打包
以下打包非常暴力.O(∩_∩)O哈哈~ 方法一: 第一步:选择需要打包的程序 第二步:选择需要打包的文件 第三步:artifacts->jar->from modules with... ...
- 浏览器和服务器 对http请求(post get) url长度限制
1. GET URL长度限制 在Http1.1协议中并没有提出针对URL的长度进行限制,RFC协议里面是这样描述的,HTTP协议并不对URI的长度做任何的限制,服务器端 必须能够处理任何它们所提供服 ...
- kafka消费数据策略
单线程消费 以之前生产者中的代码为例,事先准备好了一个 Topic:data-push,3个分区. 先往里边发送 100 条消息,没有自定义路由策略,所以消息会均匀的发往三个分区. 先来谈谈最简单的单 ...
- css文件的MIME错误引发的Jquery Mobile绘制错误
静态文件serve设置的MIME不对,引起的浏览器警告 Resource interpreted as Stylesheet but transferred with MIME type applic ...
- SpringBoot------Maven Clean报错
报错信息: Plugin org.apache.maven.plugins:maven-clean-plugin: or one of its dependencies could not be re ...
- [Android] 基于 Linux 命令行构建 Android 应用(二):命令行管理项目
创建 Android 项目 在命令行创建 Android 项目需要用到 android 工具(该工具由 Android SDK 提供,位于 <sdk>/tools/ 目录下.).它能自动生 ...