当前位置:网站首页>力扣解法汇总1374-生成每种字符都是奇数个的字符串
力扣解法汇总1374-生成每种字符都是奇数个的字符串
2022-08-01 10:24:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
https://github.com/September26/java-algorithms
原题链接:
描述:
给你一个整数 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
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 判断n是否是偶数,如果是偶数则最后一位加b,否则加a
代码:
public class Solution1374 {
public String generateTheString(int n) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i < n - 1) {
builder.append("a");
continue;
}
if (i % 2 != 0) {
builder.append("b");
} else {
builder.append("a");
}
}
return builder.toString();
}
}边栏推荐
- Taobao commodity details and details on taobao, senior upgrade version of the API
- MacOS下postgresql(pgsql)数据库密码为什么不需要填写或可以乱填写
- IntellJ IDEA如何显示换行符(line endings)
- What is a stepper motor?40 pictures to show you!
- 图解MySQL内连接、外连接、左连接、右连接、全连接......太多了
- STM32 Personal Notes - Embedded C Language Optimization
- 广域铭岛入选2022年重庆市数字经济产业发展试点示范项目名单
- PDMan-domestic free general database modeling tool (minimalist, beautiful)
- Glassmorphism design style
- 7. SAP ABAP OData 服务如何支持 $orderby (排序)操作
猜你喜欢

VS“无法查找或打开PDB文件”是怎么回事?如何解决

CTFshow,命令执行:web34、35、36

Google Earth Engine APP——15行代码搞定一个inspector高程监测APP

如何解决 chrome 浏览器标签过多无法查看到标题的情况

jmeter

Mini Program Graduation Works WeChat Food Recipes Mini Program Graduation Design Finished Products (3) Background Functions

PowerPC技术与市场杂谈

阿里腾讯面试一二

Drawing arrows of WPF screenshot control (5) "Imitation WeChat"

记一次 .NET 某智慧物流WCS系统CPU爆高分析
随机推荐
Pve delete virtual machine "for a collection"
July 31, 2022 -- Take your first steps with C# -- Use arrays and foreach statements in C# to store and iterate through sequences of data
retired paddling
Mysql索引相关的知识复盘一
jmeter
STM32 Personal Notes - Watchdog
Enterprise WeChat group: robot timing reminder function database configuration
Visualization - Superset installation and deployment
Batch大小不一定是2的n次幂!ML资深学者最新结论
正则表达式
CTFshow,命令执行:web37
使用ESP32驱动QMA7981读取三轴加速度(带例程)
Opencv creates a window - cv.namedWindow()
基于ModelArts的物体检测YOLOv3实践【玩转华为云】
记一次 .NET 某智慧物流WCS系统CPU爆高分析
Yang Hui Triangle (C language implementation)
企业微信群:机器人定时提醒功能数据库配置化
可视化——Superset安装与部署
Guangyu Mingdao was selected into the list of pilot demonstration projects for the development of digital economy industry in Chongqing in 2022
自定义类型——枚举、联合