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,由空格分隔的单词组成.每个单词仅由小写和大写 ...
随机推荐
- C++ | 虚函数初探
虚函数 虚函数 是在基类中使用关键字 virtual 声明的函数.在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数. 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调 ...
- 模型预测控制(MPC)简介
1.引言 在当今过程控制中,PID当然是用的最多的控制方法,但MPC也超过了10%的占有率.MPC是一个总称,有着各种各样的算法.其动态矩阵控制(DMC)是代表作.DMC采用的是系统的阶跃响应曲线,其 ...
- 推荐一款强大的轻量级模块化WEB前端快速开发框架--UIkit
前言 今天给大家分享一款强大的轻量级模块化WEB前端快速开发框架--UIkit 到目前(2016-06-20)为止,UIkit在github上的Forks已达到了1350个,而Stars更是达到了69 ...
- js如何获取iframe页面内的对象
简单介绍iframe标签,所有的浏览器都支持<iframe>标签,iframe 元素会创建包含另外一个文档的内联框架(即行内框架).通常我们常用的iframe标签的属性有:width(if ...
- 利用angular4和nodejs-express构建一个简单的网站(七)—用户注册之ReactiveForm
这一节对用户注册组件进行分析.用户注册组件主要涉及angular4表单的操作.Angular提供了两种构建表单的技术:响应式表单和模板驱动表单. 这两项技术都属于@angular/forms库,并且共 ...
- 利用Charles做代理测试电脑上写的H5页面
做H5页面的同学可能经常会遇到一个场景,就是电脑上调试好的页面怎么在手机上访问测试呢? 下面就介绍一种自己经常使用的方式,利用Charles代理软件来实现! 安装Charles 直接去官网下载对应的系 ...
- 【Android开发】安卓炫酷效果集合
1. android-ripple-background 能产生波浪效果的背景图片控件,可以自定义颜色,波浪扩展的速度,波浪的圈数. github地址 2. android-shapeLoadingV ...
- CentOS7 Network Setting
#display devices[root@localhost ~]# nmcli d #set ipv4 address[root@localhost ~]# nmcli c modify eth0 ...
- 引用nodejs的url模块实现url路由功能
我们在本地创建服务器之后需要写不同的后缀名来访问同一个站点的不同页面,如果不实现路由功能.则每次访问localhost:3000 不论后面写什么 比如localhost:3000/index.loc ...
- 编译python(cpython)的源码及其用途
获取python的源码 3.x及最新版本的源码:https://github.com/python/cpython python2.7分支的源码:https://github.com/python/c ...