当前位置:网站首页>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"
}
边栏推荐
- 高位套牢机构,用友网络的信任危机是如何产生的?
- tensorflow-gpu2.4.1安装配置详细步骤
- 利用 rpush 和 blpop 实现 Redis 消息队列
- Brush the topic of mobile zero power button
- ECCV 2022 Oral | 满分论文!视频实例分割新SOTA: IDOL
- ECCV2022 | 用于视频问题回答的视频图Transformer
- Use ControlTemplate or Style from resource file in WPF .cs and find the control
- C中的数据存储
- FreeRTOS Intermediate
- CentOS 7 安装mysql
猜你喜欢

Jingdong cloud released a new generation of distributed database StarDB 5.0

In-depth understanding of JVM-memory structure

钱江摩托某型号产品ECU货不对版 消费者知情权应如何保障?

【飞控开发高级教程4】疯壳·开源编队无人机-360 度翻滚

LeetCode 952. 按公因数计算最大组件大小

1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)

Anaconda 虚拟环境迁移

【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)

头条服务端一面经典10道面试题解析

Shell编程之循环语句
随机推荐
FreeRTOS中级篇
那些年我写过的语言
Unity gets the actual coordinates of the ui on the screen under the canvas
Node version switching tool NVM and npm source manager nrm
一种能有效缓解环境噪声对音频质量干扰的方案
Statistical machine learning 】 【 linear regression model
The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
ThreadLocal详解
嵌入式分享合集27
演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”
Detailed explanation of JWT
京东云发布新一代分布式数据库StarDB 5.0
数据驱动的软件智能化开发| ChinaOSC
redis常用命令,HSET,XADD,XREAD,DEL等
高效目标检测:动态候选较大程度提升检测精度(附论文下载)
ESP8266-Arduino编程实例-MCP4725数模转换器驱动
【统计机器学习】线性回归模型
揭秘5名运维如何轻松管理数亿级流量系统
(十六)51单片机——红外遥控
【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停