当前位置:网站首页>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
边栏推荐
- The elder brother of the goldfish RHCA memoirs: CL210 experiment management it network - chapter
- University of California | Inverse Reinforcement Learning from Different Third-Person Videos via Graph Abstraction
- CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) 题解
- LeetCode 0152. 乘积最大子数组:dp + 原地滚动
- 成都理工大学&电子科技大学|用于强化学习的域自适应状态表示对齐
- C#/VB.NET:从 PDF 文档中提取所有表格
- Use of message template placeholders
- Friends who want to use the database anytime, anywhere and as you like, all attention!
- 2022年 PHP面试问题记录
- 【Day_10 0428】井字棋
猜你喜欢

【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)

How to build a CMDB driven by consumption scenarios?

explain 各字段介绍

el-form-item prop属性动态绑定不生效如何解决

【Day_12 0507】二进制插入

QPalette palette, frame color fill

MySQL数据库————流程控制

C#/VB.NET: extracted from the PDF document all form

ExcelPatternTool: Excel表格-数据库互导工具

云原生全景图详解
随机推荐
Summer vacation second week wrap-up blog
【Translation】OpenMetrics cultivated by CNCF becomes an incubation project
云原生全景图详解
Use of message template placeholders
暑假第二周总结博客
OpenCV installation, QT, VS configuration project settings
粒子滤波 particle filter —从贝叶斯滤波到粒子滤波——Part-I(贝叶斯滤波)
golang json 返回空值
Leetcode71. 简化路径
Leetcode75. 颜色分类
XAML WPF项目groupBox控件
解决MySQL插入不了中文数据问题
How to build a CMDB driven by consumption scenarios?
SQL function TO_DATE (2)
【Error】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat’)
行业沙龙第二期丨如何通过供应链数字化业务协同,赋能化工企业降本增效?
SQL函数 TO_DATE(二)
Break the performance ceiling!AsiaInfo database supports more than 1 billion users, with a peak of one million transactions per second
B002 - 基于嵌入式的老人定位追踪监测仪
MySQL 45 Talk | 09 How to choose common index and unique index?