当前位置:网站首页>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
边栏推荐
- 顺序表的简单描述及代码的简单实现
- 行业沙龙第二期丨如何通过供应链数字化业务协同,赋能化工企业降本增效?
- SQL function TO_DATE (1)
- 请你说说多线程
- 2022,程序员应该如何找工作
- WinRAR | Generate multiple installers into one installer
- Three solutions: npm WARN config global --global, --local are deprecated. Use --location=global instead.
- How to build a CMDB driven by consumption scenarios?
- QT_QThread thread
- Leetcode75. Color Classification
猜你喜欢
随机推荐
Prometheus的Recording rules实践
B005 - STC8 based single chip microcomputer intelligent street light control system
typora操作手册
B005 – 基于STC8的单片机智能路灯控制系统
亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
2022年 PHP面试问题记录
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) 题解
QT_Event class
SQL function TO_DATE (2)
计算IoU(D2L)
SQL函数 TO_DATE(一)
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
B011 - 51-based multifunctional fingerprint smart lock
【Day_08 0426】两种排序方法
MySQL关系型数据库事务的ACID特性与实现方法
Leetcode71. Simplified Paths
用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
C#/VB.NET: extracted from the PDF document all form
Use of message template placeholders
SQL的ROUND函数用法及其实例