当前位置:网站首页>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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。时间空间复杂度
边栏推荐
- Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
- 【Dart】dart构造函数学习记录(含dart单例模式写法)
- 附录A printf、varargs与stdarg A.2 使用varargs.h来实现可变参数列表
- 技能大赛训练:A部分加固题目
- Common pits in the Go language
- LinkedList source code sharing
- 有点奇怪!访问目的网址,主机能容器却不行
- 附录A printf、varargs与stdarg A.1 printf函数族
- 1374. 生成每种字符都是奇数个的字符串 : 简单构造模拟题
- C专家编程 第1章 C:穿越时空的迷雾 1.5 今日之ANSI C
猜你喜欢

excel高级绘图技巧100讲(二十二)-如何对不规则数据进行分列

仿牛客论坛项目

算法---解码方法(Kotlin)

R语言 线性回归的有关方法

Interview assault 70: what is the glue bag and a bag?How to solve?

MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘

Buttons with good user experience should not have hover state on mobile phones

C专家编程 前言

技能大赛训练:A部分加固题目

Digital twin Beijing the imperial palace, yuan universe is the process of tourism
随机推荐
StringTable详解 串池 性能调优 字符串拼接
C专家编程 第1章 C:穿越时空的迷雾 1.1 C语言的史前阶段
Buttons with good user experience should not have hover state on mobile phones
C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
Pytorch学习记录(八):生成对抗网络GAN
附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
【luogu P1912】诗人小G(二分栈)(决策单调性优化DP)
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,有什么方案?
测试的意义并不是能找到全部的缺陷
Protocol Buffer 使用
技能大赛训练:A部分加固题目
分类接口,淘宝分类详情 API
MySQL语法基础
织梦发布文章提示body has not allow words错误
漏洞分析丨HEVD-0x6.UninitializedStackVariable[win7x86]
C陷阱与缺陷 第7章 可移植性缺陷 7.6 内存位置0
Digital twin Beijing the imperial palace, yuan universe is the process of tourism
网络安全与基础设施安全局(CISA):两国将在网络安全方面扩大合作
tiup mirror clone
STAHL触摸屏维修一体机显示屏ET-316-TX-TFT常见故障