824. Goat Latin - LeetCode
Questioin

Solution
题目大意:根据要求翻译句子
思路:转换成单词数组,遍历数组,根据要求转换单词
Java实现:
用Java8的流实现,效率太低
public String toGoatLatin(String S) {
final String[] arr = S.split(" ");
final int[] idx = {0};
return Arrays.stream(S.split(" "))
.map(s -> convert(s, ++idx[0]))
.reduce("", (s1, s2) -> s1 + " " + s2).trim();
}
String convert(String ori, int count) {
String pre = "";
// begin with vowel aeiou
char first = ori.charAt(0);
if (first == 'A' || first == 'a'
|| first == 'E' || first == 'e'
|| first == 'I' || first == 'i'
|| first == 'O' || first == 'o'
|| first == 'U' || first == 'u'
) {
pre = ori;
} else {
// begin with consonant not aeiou
pre = ori.substring(1) + first;
}
// add a
char[] a = new char[count];
for (int i = 0; i < count; i++) {
a[i] = 'a';
}
return pre + "ma" + String.valueOf(a);
}

public String toGoatLatin(String S) {
StringBuilder sb = new StringBuilder();
int count = 1;
for(String tmp : S.split(" ")) {
sb.append(convert(tmp, count++)).append(" ");
}
return sb.toString().trim();
}

824. Goat Latin - LeetCode的更多相关文章
- 【Leetcode_easy】824. Goat Latin
problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...
- 【LeetCode】824. Goat Latin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 824. Goat Latin山羊拉丁文
[抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...
- LeetCode 824. Goat Latin (山羊拉丁文)
题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...
- 824. Goat Latin
class Solution { public: string toGoatLatin(string S) { S.push_back(' '); //add a space that the loo ...
- LeetCode算法题-Goat Latin Easy(Java实现)
这是悦乐书的第322次更新,第344篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第192题(顺位题号是824).给出句子S,由空格分隔的单词组成.每个单词仅由小写和大写 ...
随机推荐
- JavaScript 遍历对象、数组总结
在日常工作过程中,我们对于javaScript遍历对象.数组的操作是十分的频繁的,今天抽空把经常用到的方法小结一下,方便今后参考使用! javaScript遍历对象总结 1.使用Objec ...
- 用 JS(JavaScript )实现多选、全选、反选
JS小例题 学习内容: 需求 总结: 学习内容: 需求 用 JavaScript 实现全选.反选.多选 实现代码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 前端如何通过js判断浏览器的类型(忽略版本)web html css javascript
每个页面浏览器会实例出一个window对象,在window对象下有一个属性navigator,navigator本身是一个对象,navigator对象上有一个属性userAgent里面包含了当前浏览器 ...
- 微信小程序列表拖动排序Demo
wxml页面编写 <view class="container"> <view bindtap="box" class="box&q ...
- Docker 核心知识回顾
Docker 核心知识回顾 最近公司为了提高项目治理能力.提升开发效率,将之前的CICD项目扩展成devops进行项目管理.开发人员需要对自己的负责的项目进行流水线的部署,包括写Dockerfile ...
- js知识梳理5:关于函数的要点梳理(1)
写在前面 注:这个系列是本人对js知识的一些梳理,其中不少内容来自书籍:Javascript高级程序设计第三版和JavaScript权威指南第六版,感谢它们的作者和译者.有发现什么问题的,欢迎留言指出 ...
- 6.S081-2021-Lab3 Pgtbl学习笔记
Speed up system calls 根据hints查看kernel/proc.c中的函数proc_pagetable // kernel/proc.c // Create a user pag ...
- 异步任务-springboot
异步任务-springboot 异步:异步与同步相对,当一个异步过程调用发出后,调用者在没有得到结果之前,就可以继续执行后续操作.也就是说无论异步方法执行代码需要多长时间,跟主线程没有任何影响,主线程 ...
- netty系列之:java中的base64编码器
简介 什么是Base64编码呢?在回答这个问题之前,我们需要了解一下计算机中文件的分类,对于计算机来说文件可以分为两类,一类是文本文件,一类是二进制文件. 对于二进制文件来说,其内容是用二进制来表示的 ...
- 原生的ajax请求
原生ajax请求的步骤: get 请求: 1,创建一个xhr变量 var xhr=new XMhttpRequest(); 2,设置请求方式和请求地址 xhr.open('url','http//19 ...