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 ...
随机推荐
- InpOutx64 替代 winIOx64
/***************************************************************************** * InpOutx64 替代 winIOx ...
- WDF - CSS 书写规范
CSS已经写了很久了,但是感觉代码还是有点乱,不够漂亮.今天抽点时间整理一下手头上正在做的网站样式,顺带做一个自己比较适应的书写规范,以供以后参考.先暂时这样吧,其他以后再完善. 逻辑:大小 → 位置 ...
- Java得到当前系统时间,精确到毫秒的几种方法
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Ma ...
- .NET面试题总结
1.c#垃圾回收机制 从以下方面入手展开: 1.压缩合并算法 2.代的机制 3.GC调用终结器 2.委托和事件 先说它的定义:委托的本质是类,类型安全的指针,然后从用途上考虑,事件是包装的委托 ...
- word中如何将空格变成换行
大家在工作和学习中可能会遇到文字替换或符号替换,大家要学会txt.doc.xls之间的切换,替换好之后放到最终的文件中,txt好处是没有格式,doc个好处是有格式,而xls主要是分配到单元格中. 那么 ...
- Dropping tests(二分加01数字)
个人心得:不能说题目太难,而是自己思维太菜,我开始以为这怎么都想不到的,但是学长说不是很简单吗,好吧我信了,我太low. 其实单纯二分只用于搜索,但是这种逆向答案二分确实比较难理解.给大神代码 [一些 ...
- springboot 自定义属性
前言 spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们如何添加呢 1.添加自定义属性 在src/main/resources/a ...
- ansible playbook 使用
ansible playbook 格式是json yaml 1. 执行方法 ansible-playbook deply.yml 2. playbook 格式 关键字 hosts 主机ip 主机 ...
- 前端mvc mvp mvvm 架构介绍(vue重构项目一)
首先 我们为什么重构这个项目 1:我们现有的技术是前后台不分离,页面上采用esayUI+jq构成的单页面,每个所谓的单页面都是从后台胜场的唯一Id 与前端绑定,即使你找到了那个页面元素,也找不到所在的 ...
- C 游戏所要看的书
C 游戏所要看的书 1.C++primer中文版第4版 经典啊2.C++标准程序库自修教程与参考手册 3.Windows程序设计第5版 4.MFC windows程序设计第2版中文版 5.VC ...