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,由空格分隔的单词组成.每个单词仅由小写和大写 ...
随机推荐
- Visual Studio App Center 中的 Bug 跟踪服务
我在之前的一篇文章 <使用 Visual Studio App Center 持续监视应用使用情况和问题> 中介绍了 App Center 的基本功能及使用入门,其中 诊断 可以自动手机用 ...
- 解决联想笔记本 安装VM虚拟机后每次启动都会导致电脑蓝屏问题
现象描述: pc为联想笔记本 系统是微软家庭中文版,每次启动VM虚拟机都会出现蓝屏现象,出现错误代码system_service_exception 原因及解决方法: 对于Windows10家庭版 ...
- 体验js之美第八课-面向对象创建和继承终结篇
概述 到这里我们讲说js面向对象的系列部分的最后一个课程,面向对象必须掌握两个东西一个是对象的创建一个是继承.这节课我们重点说说这两个问题最后我们说下在ES6里面面向对象怎么玩. 1对象的创建 我们第 ...
- HTML5中新增Javascript特性
存储 localStorage 存储: window.localStorage.setItem('key', 'value'); 取值: window.localStorage.getItem('ke ...
- CSS的inline、block与inline-block
基本知识点 行内元素一般是内容的容器,而块级元素一般是其他容器的容器,行内元素适合显示具体内容,而块级元素适合做布局. 块级元素(block):独占一行,对宽高的属性值生效:如果不给宽度,块级元素就默 ...
- C#委托、多播委托极简案例,一看就懂
废话不多讲,直接上代码,看完代码再讲解: class Class1 { public delegate void Del();//声明委托 public static void F1() { Cons ...
- python根据日期判断星期几(超简洁)
代码: from datetime import datetime def getWeek(week): print(date + "是星期" + str(week + 1)) d ...
- java中请给一个Abstract类实现接口的实例!
2.Abstract类实现接口 马克-to-win:如果实现某接口的类是abstract类,则它可以不实现该接口所有的方法.但其非abstract的子类中必须拥有所有抽象方法的实在的方法体:(当然它a ...
- 彻底理解volatile关键字
1. volatile简介 在上一篇文章中我们深入理解了java关键字,我们知道在java中还有一大神器就是关键volatile,可以说是和synchronized各领风骚,其中奥妙,我们来共同探讨下 ...
- HDMS(Headend Device Management System)软件下载
进入官网http://www.pbi-china.com/CHS/index.aspx点击右下角的下载通道.