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. 学习移动机器人SLAM、路径规划必看的几本书

    作者:小白学移动机器人链接:https://zhuanlan.zhihu.com/p/168027225来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 声明:推荐正版图 ...

  2. C与C++的区别之函数调用堆栈

    函数调用栈 1.函数参数带入(入调用方函数的栈,从右向左入栈) int fun(int a); int fun(int a, int b); int fun(int a, int b, int c); ...

  3. 实用的 CSS — 贝塞尔曲线(cubic-bezier)

    欢迎移步我的博客阅读:<实用的 CSS - 贝塞尔曲线(cubic-bezier)> 前言 在了解 cubic-bezier 之前,你需要对 CSS3 中的动画效果有所认识,它是 anim ...

  4. 微信小程序:自定义组件的数据传递

    一.前言 如果小程序中有可复用的UI且具有一定的功能性,就可以使用自定义组件将其封装起来.下面介绍一个简单的组件和一个复杂的组件. 二.简单的组件(计数器) 1. 组件功能介绍 这个组件常见于外卖软件 ...

  5. 修改Menu_item的字体属性

    前面一直在找 MenuItem的文字颜色的设置.我发现API中只有背景颜色的设置... 所以找到下面的方法.在OverFlow上看到的.在onCreateOptionsMenu中覆写一下, 使Menu ...

  6. JSDOM基础

    JavaScript 通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素. HTML DOM 模型被构造为对象的树: HTML DOM 树 JavaScript 能够改变页 ...

  7. JS 实现下拉框去重

    JS 实现下拉框去重 学习内容: 需求 总结: 学习内容: 需求 用 JS 下拉框去重 实现代码 <html> <head> <meta http-equiv=" ...

  8. MySQL数据库设置编码格式和时区

    MySQL数据库设置编码格式和时区 MySQL5版本: url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8 MySQL6版本及以上 ...

  9. executeFind()方法和execute()方法区别

    返回类型不同:executeFind()方法返回集合,execute()方法返回对象 executeFind @Override public List<TCpContact> getCp ...

  10. python---运算符和编码

    格式化输出 在输出数据时,会有用户需要输入的信息被输出,没办法预先知道,这就需要使用格式化输出.先在相应位置放置一个占位符,再把字符串里的占位符与外部的变量做个映射关系. name = input(& ...