当前位置:网站首页>LeetCode 1374. 生成每种字符都是奇数个的字符串
LeetCode 1374. 生成每种字符都是奇数个的字符串
2022-08-03 19:52:00 【JIeJaitt】
给你一个整数 n,请你返回一个含n个字符的字符串,其中每种字符在该字符串中都恰好出现 奇数次。
返回的字符串必须只含小写英文字母。如果存在多个满足题目要求的字符串,则返回其中任意一个即可。
示例 1:
输入:n = 4 输出:"pppz" 解释:"pppz" 是一个满足题目要求的字符串,因为 'p' 出现 3 次,且 'z' 出现 1 次。当然,还有很多其他字符串也满足题目要求,比如:"ohhh" 和 "love"。
示例 2:
输入:n = 2 输出:"xy" 解释:"xy" 是一个满足题目要求的字符串,因为 'x' 和 'y' 各出现 1 次。当然,还有很多其他字符串也满足题目要求,比如:"ag" 和 "ur"。
示例 3:
输入:n = 7 输出:"holasss"
提示:
1 <= n <= 500
当 n 为奇数时,我们返回 n 个 ‘a’ 组成的字符串。
当 n 为偶数时,我们返回 n−1 个 ‘b’ 组成的字符串。
- 时间复杂度:O(n)。
- 空间复杂度:O(1)。这里不计入返回值需要的空间。
class Solution {
public:
string generateTheString(int n) {
if (n % 2 == 1) {
return string(n, 'a');
}
return string(n - 1, 'a') + 'b';
}
};
func generateTheString(n int) string {
if n%2 == 1 {
return strings.Repeat("a", n)
}
return strings.Repeat("a", n-1) + "b"
}
边栏推荐
- 按需视觉识别:愿景和初步方案
- Statistical machine learning 】 【 linear regression model
- 傅里叶变换(深入浅出)
- Execute the mysql script file in the docker mysql container and solve the garbled characters
- pytorch框架实现老照片修复功能详细演示(GPU版)
- dpkg强制安装软件
- Line the last time the JVM FullGC make didn't sleep all night, collapse
- 一种能有效缓解环境噪声对音频质量干扰的方案
- Postgresql-xl global snapshot and GTM code walking (branch line)
- Standard C language learning summary 11
猜你喜欢

async 和 await 原来这么简单

亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得

LeetCode 622. Designing Circular Queues

Reveal how the five operational management level of hundreds of millions of easily flow system

Anaconda virtual environment migration

CS免杀姿势

盘点在线帮助中心对企业能够起到的作用

【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制

数据驱动的软件智能化开发| ChinaOSC

宁德时代2号人物黄世霖辞任副董事长:身价1370亿
随机推荐
入门3D建模基础教程详细分解
Solution for no navigation bar after Word is saved as PDF
C中的数据存储
ECCV2022 | 用于视频问题回答的视频图Transformer
子树的大小
tensorflow-gpu2.4.1安装配置详细步骤
不知道这4种缓存模式,敢说懂缓存吗?
X86 function call model analysis
Execute the mysql script file in the docker mysql container and solve the garbled characters
开源生态研究与实践| ChinaOSC
Line the last time the JVM FullGC make didn't sleep all night, collapse
2022 CCF中国开源大会会议通知(第三轮)
使用 ReportLab 绘制 PDF
622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
FreeRTOS Intermediate
数据驱动的软件智能化开发| ChinaOSC
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
MySQL master-slave, 6 minutes you master!
标准C语言学习总结11
Postgresql-xl global snapshot and GTM code walking (branch line)