LeetCode 824. Goat Latin

LeetCode --- 824. Goat Latin 解题报告 A sentenceSis 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 foll... 阅读详情

题目链接:https://leetcode.com/problems/goat-latin/description/

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.

题目解析:对每个字符进行处理,标记前一个位置是否为空格,如果是开头第一个字符就单独处理,记录非元音字符即可。在空格和最后塞入后缀,如果有非元音开头字符的话,先加上该字符。

代码如下:0ms Accepted beating 100%

class Solution {
public:
    string toGoatLatin(string S) {
        string ans;
        string ss = "maa";
        char c = '\0';
        bool isBlank = true;
        for (int i = 0; i <= S.size(); i++)
        {
            char cur = S[i];
            if (cur == ' ' || i == S.size())
            {
                if (c != '\0')
                {
                    ans.push_back(c);
                    c = '\0';
                }
                ans.append(ss);
                if (cur == ' ')
                {
                    ans.push_back(' ');
                    isBlank = true;
                }
                ss += "a";
            }
            else
            {
                if (isBlank && !(cur == 'a' || cur == 'A' || cur == 'e' || cur == 'E' || cur == 'i' || cur == 'I' || cur == 'o' || cur == 'O' || cur == 'u' || cur == 'U'))
                {
                    c = cur;
                    isBlank = false;
                    continue;
                }
                isBlank = false;
                ans.push_back(cur);
            }
        }
        return ans;
    }
};

 

WebGoat-5.4 WebGoat是OWASP组织研制出的用于进行web漏洞实验的应用平台,用来说明web应用中存在的安全漏洞。WebGoat运行在带有java虚拟机的平台之上,当前提供的训练课程有30多个,其中包括:跨站点脚本攻击(XSS)、访问控制、线程安全、操作隐藏字段、操纵参数、弱会话cookie、SQL盲注、数字型SQL注入、字符串型SQL注入、web服务、Open Authentication失效、危险的HTML注释等等。WebGoat提供了一系列web安全学习的教程,某些课程也给出了视频演示,指导用户利用这些漏洞进行攻击。是学习web漏洞实验的最佳平台(个人认为) 立即下载

相关推荐

PPO(Proximal Policy Optimization)算法原理及实现,详解近端策略优化

接着上面的讲,PG方法一个很大的缺点就是参数更新慢,因为我们每更新一次参数都需要进行重新的采样,这其实是中on-policy的策略,即我们想要训练的agent和与环境进行交互的agent是同一个agent;与之对应的就是off-policy的策略,即想要训练的agent和与环境进行交互的agent不是同一个agent,简单来说,就是拿别人的经验来训练自己。举个下棋的例子,如果你是通过自己下棋来不断提升自己的棋艺,那么就是on-policy的,如果是通过看别人下棋来提升自己,那么就是off-policy的。

weixin_41106546的博客 9万+

LeetCode 824 Goat Latin

LeetCode 824 Goat Latin 传送门 题目分析 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 sen...

博客 1154

DDR4技术原理详解

DDR4是DDR4-SDRAM的简写,即第4代DDR-SDRAM,DDR-SDRAM全称Double Date Rate Synchronous Dynamic Random Access Memory。本文重点解析DDR4技术规范及其背后的电子电路原理,以及编程使用DDR4过程中需要注意的性能事项。

ctbinzi的专栏 1万+

LeetCode 824. 山羊拉丁文

随机一题给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。山羊拉丁文的规则如下:如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。例如,单词"apple"变为"applema"。如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加...

菜鸟程序猿的博客 1074

避坑指南:Qwen2.5-VL图片预处理中常见的5个尺寸错误

本文深入解析了Qwen2.5-VL模型在图片预处理环节中开发者最常遇到的五个尺寸错误,包括动态分辨率对齐、宽高比超标、像素数限制等问题。文章结合源码解读,详细剖析了`process_vision_info`函数和`resize`逻辑,并提供了具体的排查流程图与参数设置建议,旨在帮助开发者构建健壮的预处理流水线,有效规避显存溢出与推理失败的风险。

weixin_29290379的博客 178

用OpenNI2获取奥比中光Astra Pro及 Astra Pro SM输出的深度图(win,linux arm64 x64平台)

搞了一个奥比中光Astra Pro,想在windows平台,和linux rk3588 上获取深度信息,之前的驱动下载已经不好用了。

babytiger的专栏 1309

LeetCode 题解之 824. Goat Latin

824. Goat Latin 题目描述和难度 题目描述: 给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。 我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。 山羊拉丁文的规则如下: 如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。 例如,单词"apple"变为"apple...

李威威的博客 854

YOLOv13改进策略【Backbone/轻量化】| 替换骨干网络 CVPR-2024 StarNet,超级精简高效的轻量化模块

本文记录的是。StarNet设计简洁,没有复杂的设计和精细调整的超参数,仅是一个 4 阶段的分层架构。并且其中能够在低维空间计算的同时考虑极高维的特征这一特性,进而提高模型精度。本文在替换骨干网络中配置了原论文中的starnet_s1starnet_s2starnet_s3和starnet_s4七种模型,以满足不同的需求。

Limiiiing的博客 672

[leetcode] 824. Goat Latin

题目: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 si...

jing16337305的博客 401

Python 实战:手语翻译系统——从视频到文本的智能转换

今天,我们用 Python 构建了一个。

Echo_Wish 345

【产品】 产品设计:ID设计和MD设计详解

1、ID(Industry Design)工业设计 包含外观、材质、手感、颜色配搭,主要界面的实现与及色彩等方面的设计。 2、MD(Mechanical Design)结构设计 以手机为例: 前壳、后壳、手机的摄像镜头位置的选择、固定的方式、电池如何连接和手机的厚薄程度等。 如果是滑盖手机,如何让手机滑上去,怎样实现自动往上弹,SIM卡怎样插和拔的安排,这些都是手机结构设计的范畴。 繁...

产品线负责人 3万+

Leetcode学习笔记:#824. Goat Latin

Leetcode学习笔记:#824. Goat Latin 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 L...

ccystewart的博客 308

21、使用 FastHenry 进行片上电感器的模拟与参数预测

本文介绍了使用 FastHenry 对片上电感器进行模拟和参数预测的方法。讨论了 FastHenry 的适用场景、线圈几何形状的选择与离散化方法,以及如何考虑模拟中的寄生效应。同时对比了 FastHenry 和 Magnet 在不同基板电阻率下的模拟精度,并提出了优化电感器设计的策略,为高效准确地预测电感参数提供了技术指导。

y9z0a1b的博客 296

LeetCode //C - 824. Goat Latin

This problem converts a given English sentence into Goat Latin by applying specific transformation rules to each word. The solution processes each word by checking if it starts with a vowel or consonant, then appends "ma" and adds 'a's based on t

Made in Code 1038

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

leetcode 824. Goat Latin

leetcode 824. Goat Latin 原题地址:https://leetcode.com/problems/goat-latin/ 题目 A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters on...

birdreamer的博客 619

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

[Leetcode] 824. Goat Latin

Description: 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-u...

ApRiLLLLLLLLLLLL的博客 269

LeetCode刷题记录7——824. Goat Latin(easy)

LeetCode刷题记录7——824. Goat Latin(easy) 目录 LeetCode刷题记录7——824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输入的字符串转化为Goat Latin——山羊拉丁语,有三种转化规则: 如果单词的第一个字符是元音字母(aeiou或者AEIOU),那么保留这个单词的原本格式,并在最后加上ma...

Jmuyjl 693

python leetcode 824. Goat Latin

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 simil...

DreamerLHQ的博客 990

[leetcode]824. Goat Latin

[leetcode]824. Goat Latin Analysis 周一快乐~—— [ummm~] A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to...

weixin_32135877的博客 148

LeetCode 824 Goat Latin 解题报告

题目要求 A sentenceSis 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 langua...

weixin_30312563的博客 151

【Leetcode】824. Goat Latin

题目地址: https://leetcode.com/problems/goat-latin/ 给定一个英文字符串,由单词和单词之间的空格构成,要求将每个单词按如下规则做出改变: 1、如果其以元音开头(aeiouAEIOUaeiouAEIOUaeiouAEIOU),则将其后面加上mamama; 2、如果不以元音开头,则将第一个字母置于最后,其后面加上mamama; 3、执行完上面的操作之后,第几个单词就后面跟几个aaa(从111开始计数)。 返回结果。 代码如下: public class Solution

数学、算法爱好者的博客 271
上一篇: LeetCode 58. Length of Last Word
下一篇: 台大机器学习基石 Lecture 5 - Training versus Testing
ZayneHuang
博客等级 码龄10年 5粉丝 116原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值