Questioin

824. Goat Latin

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的更多相关文章

  1. 【Leetcode_easy】824. Goat Latin

    problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...

  2. 【LeetCode】824. Goat Latin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. LeetCode 824 Goat Latin 解题报告

    题目要求 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase a ...

  4. [LeetCode&Python] Problem 824. Goat Latin

    A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...

  5. [LeetCode] 824. Goat Latin

    Description A sentence S is given, composed of words separated by spaces. Each word consists of lowe ...

  6. 824. Goat Latin山羊拉丁文

    [抄题]: A sentence S is given, composed of words separated by spaces. Each word consists of lowercase ...

  7. LeetCode 824. Goat Latin (山羊拉丁文)

    题目标签:String 首先把vowel letters 保存入 HashSet. 然后把S 拆分成 各个 word,遍历每一个 word: 当 word 第一个 字母不是 vowel 的时候,把第一 ...

  8. 824. Goat Latin

    class Solution { public: string toGoatLatin(string S) { S.push_back(' '); //add a space that the loo ...

  9. LeetCode算法题-Goat Latin Easy(Java实现)

    这是悦乐书的第322次更新,第344篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第192题(顺位题号是824).给出句子S,由空格分隔的单词组成.每个单词仅由小写和大写 ...

随机推荐

  1. (stm32f103学习总结)—待机唤醒实验

    一.STM32待机模式介绍 1.1 STM32低功耗模式介绍 很多单片机具有低功耗模式,比如MSP430.STM8L等,我们的STM32 也不例外.默认情况下,系统复位或上电复位后,微控制器进入运行模 ...

  2. c源文件中为什么要包含自己对应的头文件

    另一篇:.c文件和.h文件的关系 引言: 我们经常在c工程中发现,源文件中要包含自己的头文件.一直以来,都不知道为什么这样做.现在,我知道了. 以前的认知: 我认为,.c文件没有必要包含自己的.h文件 ...

  3. 前端每日实战:133# 视频演示如何用 CSS 和 GSAP 创作有多个关键帧的连续动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eLMKJG 可交互视频 此视频是可 ...

  4. PAT A1001 A+B Format

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  5. 前端复制粘贴文字clipBoard.js的使用

    1. vue  中的复制粘贴: <div class="mainTextItem" @click="copyTXTOne" id="copyOn ...

  6. 小程序tab栏可滑动,可点击居中demo

    效果图: 代码: <view class="container"> <!-- tab导航栏 --> <!-- scroll-left属性可以控制滚动条 ...

  7. @JsonFormat、@DateTimeFormat、@JsonSerialize注解的使用

    @JsonFormat 是jackson的注解,用于后台返回前台的时候将后台的date类型数据转为string类型格式化显示在前台,加在get方法或者date属性上面,因为 @JsonFormat 注 ...

  8. JavaWeb入门day9-随笔(session/cookie)

    会话 一个网站怎么证明用户访问过 服务端给客户端一个信件,客户端下次访问服务端带上信件就可以:cookie 服务器登记,下次访问匹配:session 保存会话的技术 cookie 客户端技术(响应.请 ...

  9. Java语言学习day30--8月5日

    ###10String类的其他方法 * A:String类的其他方法 * a: 方法介绍 * int length(): 返回字符串的长度 * String substring(int beginIn ...

  10. Spring Retry 在SpringBoot 中的应用

    Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...