当前位置:网站首页>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"
}
边栏推荐
- Postgresql snapshot optimization Globalvis new system analysis (performance greatly enhanced)
- CS免杀姿势
- Calculation of the array serial number of Likou brush questions (one question per day 7/28)
- 「学习笔记」高斯消元
- Handler source code analysis
- CSDN帐号管理规范
- 揭秘5名运维如何轻松管理数亿级流量系统
- 【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停
- 钱江摩托某型号产品ECU货不对版 消费者知情权应如何保障?
- 【leetcode】剑指 Offer II 008. 和大于等于 target 的最短子数组(滑动窗口,双指针)
猜你喜欢

高效目标检测:动态候选较大程度提升检测精度(附论文下载)

面试突击:什么是粘包和半包?怎么解决?

Detailed demonstration pytorch framework implementations old photo repair (GPU)

入门3D建模基础教程详细分解

涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流

CS kill-free pose

Matlab论文插图绘制模板第42期—气泡矩阵图(相关系数矩阵图)

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

揭秘5名运维如何轻松管理数亿级流量系统

Line the last time the JVM FullGC make didn't sleep all night, collapse
随机推荐
net-snmp编译报错:/usr/bin/ld: cannot find crti.o: No such file or directory
子树的大小
In-depth understanding of JVM-memory structure
ADS 2023 Download Link
Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
php根据两点经纬度计算距离
pytorch框架实现老照片修复功能详细演示(GPU版)
dpkg强制安装软件
那些年我写过的语言
Reveal how the five operational management level of hundreds of millions of easily flow system
C中的数据存储
阿洛的反思
relocation R_X86_64_PC32 against,/usr/bin/ld: final link failed: Bad value
glide set gif start stop
Shell programming loop statement
Handler 源码解析
亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得
MySQL 主从,6 分钟带你掌握!
京东云发布新一代分布式数据库StarDB 5.0
Node version switching tool NVM and npm source manager nrm