LeetCode 824: (C++)山羊拉丁文

编程入门 行业动态 更新时间:2024-10-10 15:27:38

LeetCode 824: (C++)山羊<a href=https://www.elefans.com/category/jswz/34/1708232.html style=拉丁文"/>

LeetCode 824: (C++)山羊拉丁文

太久没有写过LeetCode啦,今天刚好突然想起来,就找了道题目写一下,这次是用C++写的,因为最近在复习C++,正好这道题也简单,就写了一下,可能写的不咋样,大家就参考参考就行~

题目:

 

我把一些操作都分别用了不同的函数来写,应该也好理解,就不写详细思路了,应该都可以很容易看出来,这里有一个要注意的点就是 a e i o u不光是小写,还有大写也是属于同一种情况,加上就可以了

代码:

#include<iostream>
using namespace std;
class Solution {
public:int count = 0;string add(string subSentence) {subSentence += "ma";for (int i = 0; i < count; i++) {subSentence += "a";}return subSentence;}string change(string subSentence) {if (subSentence[0] == 'a' || subSentence[0] == 'e' || subSentence[0] == 'i' || subSentence[0] == 'o' || subSentence[0] == 'u' || subSentence[0] == 'A' || subSentence[0] == 'E' || subSentence[0] == 'I' || subSentence[0] == 'O' || subSentence[0] == 'U') {subSentence = add(subSentence);}else {// 移动字母再添加char c = subSentence[0];subSentence = subSentence.substr(1, subSentence.length());subSentence += c;subSentence = add(subSentence);}return subSentence;}string toGoatLatin(string sentence) {int i = 0;string newString = "";string substring = "";int length = sentence.length();for (int k = 0; k < length; k++) {if (sentence[k] == ' ') {count += 1;newString += change(substring);newString += " ";substring = "";continue;}else if (k == (sentence.length() - 1)) {substring += sentence[k];count += 1;newString += change(substring);continue;}substring += sentence[k];}return newString;}
};int main()
{Solution solution;cout << solution.toGoatLatin("Each word consists of lowercase and uppercase letters only") << endl;return 0;
}

更多推荐

LeetCode 824: (C++)山羊拉丁文

本文发布于:2024-03-04 04:07:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1708227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拉丁文   山羊   LeetCode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!