当前位置:网站首页>LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
2022-08-01 21:03:00 【小迅想变强】
链接:https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/solution/by-xun-ge-v-hiex/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
示例
思路
题目要求生成奇数个字符,很显然分情况讨论即可
- 当n为奇数,生成 n 个 a 即可
- 当n为偶数,生成 n-1 个 a 和 任意一个其他字符即可
代码
char * generateTheString(int n){
char * str = malloc(sizeof(char) * (n+1));//定义字符串
memset(str, 97, sizeof(char) * n);//初始化为a
if(n % 2 == 0)//判断奇偶
{
str[0] = 'm';
}
str[n] = '\0';
return str;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/solution/by-xun-ge-v-hiex/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间空间复杂度
边栏推荐
猜你喜欢
Convolutional Neural Network (CNN) mnist Digit Recognition - Tensorflow
Which websites are commonly used for patent searches?
STAHL touch screen repair all-in-one display screen ET-316-TX-TFT common faults
仿牛客论坛项目
图的邻接矩阵存储
【Kaggle】Classify Leaves
写给刚进互联网圈子的人,不管你是开发,测试,产品,运维都适用
30+的女性测试人面试经验分享
MySQL语法基础
StringTable Detailed String Pool Performance Tuning String Concatenation
随机推荐
C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
C陷阱与缺陷 第5章 库函数 5.5 库函数signal
Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决
职场如象棋,测试/开发程序员如何突破成长瓶颈期?
Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data
C专家编程 第1章 C:穿越时空的迷雾 1.2 C语言的早期体验
idea插件generateAllSetMethod一键生成set/get方法以及bean对象转换
Nacos 配置中心
分类接口,淘宝分类详情 API
C专家编程 序
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
字符串
那些关于DOM的常见Hook封装(二)
360借条安全专家:陌生微信好友不要轻易加贷款推广多是诈骗
密码学的基础:X.690和对应的BER CER DER编码
C语言之字符串函数二
StringTable Detailed String Pool Performance Tuning String Concatenation
Telnet弱口令渗透测试
Digital twin Beijing the imperial palace, yuan universe is the process of tourism
win10版本1803无法升级1903系统如何解决