当前位置:网站首页>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
a
meet 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个b
meet 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
边栏推荐
- MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
- XAML WPF项目groupBox控件
- Friends who want to use the database anytime, anywhere and as you like, all attention!
- OpenCV installation, QT, VS configuration project settings
- Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
- sql添加索引
- ACID Characteristics and Implementation Methods of MySQL Relational Database Transactions
- Golang协程调度器scheduler怎么使用
- B005 - STC8 based single chip microcomputer intelligent street light control system
- 7月30号|来一场手把手助您打造智能视觉新爆款的技术动手实验
猜你喜欢
B002 - Embedded Elderly Positioning Tracking Monitor
B001 - Intelligent ecological fish tank based on STM32
MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
【Day_12 0507】查找组成一个偶数最接近的两个素数
opencv如何实现图像倾斜校正
三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.
Stop using MySQL online DDL
C language theory--a solid foundation for the written test and interview
QT基础功能,信号、槽
Leetcode71. 简化路径
随机推荐
XAML WPF item groupBox control
Friends who want to use the database anytime, anywhere and as you like, all attention!
B001 - 基于STM32的智能生态鱼缸
B011 - 51-based multifunctional fingerprint smart lock
关于单应性矩阵的若干思考
用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
突破性能天花板!亚信数据库支撑 10 多亿用户,峰值每秒百万交易
opencv语法Mat类型总结
odoo 编码规范(编程规范、编码指南)
解决MySQL插入不了中文数据问题
SQL函数 TO_DATE(一)
QT_事件类
How to solve the dynamic binding of el-form-item prop attribute does not take effect
生命周期和作用域
JVM运行时数据区与JMM内存模型是什么
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
How to use the Golang coroutine scheduler scheduler
Leetcode71. Simplified Paths
QT基础功能,信号、槽
B005 – 基于STC8的单片机智能路灯控制系统