当前位置:网站首页>「 每日一练,快乐水题 」1374. 生成每种字符都是奇数个的字符串
「 每日一练,快乐水题 」1374. 生成每种字符都是奇数个的字符串
2022-08-02 19:47:00 【谁吃薄荷糖】
力扣原题:
*题目简述:
给你一个整数 n,请你返回一个含 n 个字符的字符串,其中每种字符在该字符串中都恰好出现 奇数次 。
返回的字符串必须只含小写英文字母。如果存在多个满足题目要求的字符串,则返回其中任意一个即可。
*解题思路:
- 根据题意进行模拟;
n
为偶数时,则可以搞n-1
个a
与1
个b
的情况;n
为奇数时,则可搞n
个a
即可;- over;
*C++代码:
class Solution {
public:
string generateTheString(int n) {
string str;
if(n % 2 == 0)
{
///< 偶数
for(int i = 0; i < n -1; i++)
{
str += 'a';
}
str += 'b';
}
else
{
///< 奇数
for(int i = 0; i < n; i++)
{
str += 'a';
}
}
return str;
}
};
结果展示:
边栏推荐
猜你喜欢
一款好用的FAQ搭建工具
Three.js入门
基于 outline 实现头像剪裁以及预览
磁盘分区的知识
GNN教程:图神经网络基础知识!
In action: 10 ways to implement delayed tasks, with code!
Flutter with internationalized adapter automatically generated
分享一个 web 应用版本监测 (更新) 的工具库
MOSN 反向通道详解
ShardingSphere-proxy +PostgreSQL implements read-write separation (static strategy)
随机推荐
The so-called fighting skill again gao also afraid of the chopper - partition, depots, table, and the merits of the distributed
Introduction of uncommon interfaces of openlayers
MOSN 反向通道详解
译出我精彩 | 7月墨力翻译计划获奖名单公布
Five data structures of Redis and their corresponding usage scenarios
软考 ----- UML设计与分析(下)
Thread线程类基本使用(下)
入职对接-hm项目
如何解决图像分类中的类别不均衡问题?不妨试试分开学习表征和分类器
云平台简介
线程安全(上)
Thread线程类基本使用(上)
ShardingSphere-proxy +PostgreSQL implements read-write separation (static strategy)
Office2021 安装MathType
笑话:如果你在河边等待得足够久,你会看到你的敌人的尸体漂过,是怎么翻译出来的?
Detailed explanation of common examples of dynamic programming
2022-07-27
SQL Server数据类型转换函数cast()和convert()详解
成为黑客不得不学的语言,看完觉得你们还可吗?
js Fetch返回数据res.json()报错问题