博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode讲解--824. Goat Latin
阅读量:7082 次
发布时间:2019-06-28

本文共 1506 字,大约阅读时间需要 5 分钟。

题目

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

We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)

The rules of Goat Latin are as follows:

If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.

For example, the word 'apple' becomes 'applema'.

If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".

For example, the word "goat" becomes "oatgma".

Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.

For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.
Return the final sentence representing the conversion from S to Goat Latin.

Example 1:

Input: "I speak Goat Latin"Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"

Example 2:

Input: "The quick brown fox jumped over the lazy dog"Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

Notes:

  • S contains only uppercase, lowercase and spaces. Exactly one space between each word.
  • 1 <= S.length <= 150.

讲解

这个题目看着挺简单的,但用代码写出来还挺长的。

java代码

class Solution {    public String toGoatLatin(String S) {        StringBuilder sb = new StringBuilder();        StringBuilder a = new StringBuilder();        char[] vowel = {'a', 'e', 'i', 'o', 'u'};        int wordCount = 1;        for(int i=0;i

转载地址:http://yfmml.baihongyu.com/

你可能感兴趣的文章
「POJ3734」Blocks
查看>>
Beta 冲刺 (4/7)
查看>>
CSS3
查看>>
VGG16提取图像特征 (torch7)
查看>>
Python 远程部署 Fabric
查看>>
1013A.Piles With Stones
查看>>
multipart/form-data和application/x-www-form-urlencoded的区别
查看>>
php 搜索附近人及SQL语句的写法
查看>>
拖拽(兼容版)
查看>>
Python中and和or的运算法则
查看>>
Raspberry Pi使用USB摄像头远程监控
查看>>
Ruby-selenium-webdriver(安装准备篇)
查看>>
今天愣了半天硬是没想到用map,在此还原以下代码
查看>>
【转】UITableViewCell自适应高度 UILabel自适应高度和自动换行
查看>>
网络问卷调查js实现代码
查看>>
mark住先
查看>>
mysql事物处理
查看>>
c++中的基本知识点
查看>>
一些js在线引用文档
查看>>
关键路径
查看>>