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"
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为最后再一起添加a,其实是每一个都加好 再拼到一起的
[一句话思路]:
给元音建立新集合
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 拆开的字母串、字符串可以直接放在等号右边,更省事
- 字符串向后添加,直接用+=就行了
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
以为最后再一起添加a,其实是每一个都加好 再拼到一起的
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
for (j = 0, i++; j < i; ) 两个指针中j 每次清0,i不清零 来控制逐渐递增。头次见
[关键模板化代码]:
charat(0)第0位 && .substring(1)去除第0位之后 互相对应的
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public String toGoatLatin(String S) {
//ini:store in set, res
Set<Character> vowels = new HashSet<>();
for (char c : "aeiouAEIOU".toCharArray()) {
vowels.add(c);
}
String res = "";
int i = 0, j = 0;
//for loop
for (String word : S.split("\\s")) {
res += ' ' + (vowels.contains(word.charAt(0)) ? word : word.substring(1) + word.charAt(0)) + "ma";
//add a
for (j = 0, i++; j < i; j++) {
res += 'a';
}
}
//return
return res.substring(1);
}
}
824. Goat Latin山羊拉丁文的更多相关文章
- LeetCode 824. Goat Latin (山羊拉丁文)
题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...
- [LeetCode] Goat Latin 山羊拉丁文
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- Leetcode824.Goat Latin山羊拉丁文
给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin 的虚构语言). 山羊拉 ...
- 【Leetcode_easy】824. Goat Latin
problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...
- 824. Goat Latin - LeetCode
Questioin 824. Goat Latin Solution 题目大意:根据要求翻译句子 思路:转换成单词数组,遍历数组,根据要求转换单词 Java实现: 用Java8的流实现,效率太低 pu ...
- LeetCode 824 Goat Latin 解题报告
题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...
- [LeetCode&Python] Problem 824. Goat Latin
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Android Animation 动画
动画类型 Android的animation由四种类型组成 Android动画模式 Animation主要有两种动画模式:一种是tweened animation(渐变动画) XML中 JavaCo ...
- VMware 10安装Mac OS X 10.11和XCode7
上周把我的计算机当试验品,安装mac虚拟机.由于文件下载复制解压的时间花了很长,历时两天,记录下来(和我一样的新手不妨参考一下): 我机硬件:win7 64位 8G内存 没有8G以上就不要考虑了.我安 ...
- LeetCode Split Array into Consecutive Subsequences
原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...
- Python 中,字符串"连接"效率最高的方式是?一定出乎你的意料
网上很多文章人云亦云,字符串连接应该使用「join」方法而不要用「+」操作.说前者效率更高,它以更少的代价创建新字符串,如果用「+」连接多个字符串,每连接一次,就要为字符串分配一次内存,效率显得有点低 ...
- #51单片机#超声波测距(HC-SR04)的使用方法
#51单片机#超声波测距(HC-SR04)的使用方法
- mysql存入GBK编码字段信息
set @moneyStr=BASE64_ENCODE(CONVERT(CONCAT('线上报名且已交费',money,'元') using GBK));
- Voting and Shuffling to Optimize Atomic Operations
2iSome years ago I started work on my first CUDA implementation of the Multiparticle Collision Dynam ...
- emqtt 3 (我要subscribe 这个topic)
这一次,主要分析client subscribe 某个topic 的处理流程. 由protocol开始 是的,还是要从protocol开始,至于为什么,之前就说过了. subscribe 类型的pac ...
- SpringMVC上传文件大小的设置
在spring-mvc.xml(springmvc的配置文件)里: <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id ...
- java显示网格————————
总结:看图 +---+---+ | | | | | | +---+---+ */ package com.aaa; //在屏幕上显如下网格 public class adga { public sta ...