当前位置:网站首页>LeetCode 1374. Generate an odd number of each character string
LeetCode 1374. Generate an odd number of each character string
2022-08-01 18:20:00 【Tisfy】
【LetMeFly】1374.生成每种字符都是奇数个的字符串
力扣题目链接:https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/
给你一个整数 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
方法一:构造
- If the length of the string is odd,Then every character in the string is
ameet the requirements of the subject; - If the length of the string is even( n n n),那么 n − 1 n-1 n−1为奇数,返回 n − 1 n-1 n−1个
a和 1 1 1个bmeet the requirements of the subject.
(PS: syntactic sugar available——三木运算符 一行代码解决)
- 时间复杂度 O ( n ) O(n) O(n)
- 空间复杂度 O ( 1 ) O(1) O(1)
AC代码
C++
class Solution {
public:
string generateTheString(int n) {
return n % 2 ? string(n, 'a') : string(n - 1, 'a') + 'b';
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126093645
边栏推荐
- Leetcode73. 矩阵置零
- How to make the fixed-point monitoring equipment display the geographic location on the EasyCVR platform GIS electronic map?
- 电商库存系统的防超卖和高并发扣减方案
- Leetcode74. Search 2D Matrix
- QT基础功能,信号、槽
- BITS Pilani|SAC-AP:基于 Soft Actor Critic 的深度强化学习用于警报优先级
- WinRAR | Generate multiple installers into one installer
- Go GORM事务实例分析
- golang json 返回空值
- 公用函数----mfc
猜你喜欢
随机推荐
行业沙龙第二期丨如何通过供应链数字化业务协同,赋能化工企业降本增效?
What is the implementation principle of Go iota keyword and enumeration type
Solve the problem that MySQL cannot insert Chinese data
Leetcode75. 颜色分类
Leetcode73. Matrix Zeroing
Summer vacation first week wrap-up blog
How to build a CMDB driven by consumption scenarios?
Break the performance ceiling!AsiaInfo database supports more than 1 billion users, with a peak of one million transactions per second
QT基础功能,信号、槽
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.
QT commonly used global macro definitions
Summer vacation second week wrap-up blog
Topology Parts Disassembly 3D Visualization Solution
LeetCode 1374.生成每种字符都是奇数个的字符串
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
Clip-on multimeter use method, how to measure the voltage, current, resistance?
Golang协程调度器scheduler怎么使用
电商库存系统的防超卖和高并发扣减方案
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)









