当前位置:网站首页>Leetcode 984. 不含 AAA 或 BBB 的字符串(网友思路)
Leetcode 984. 不含 AAA 或 BBB 的字符串(网友思路)
2022-06-29 16:31:00 【我不是萧海哇~~~~】

给定两个整数 a 和 b ,返回 任意 字符串 s ,要求满足:
- s 的长度为 a + b,且正好包含a 个 ‘a’ 字母与 b 个 ‘b’ 字母;
- 子串 ‘aaa’ 没有出现在 s 中;
- 子串 ‘bbb’ 没有出现在 s 中。
示例 1:
输入:a = 1, b = 2
输出:"abb"
解释:"abb", "bab" 和 "bba" 都是正确答案。
示例 2:
输入:a = 4, b = 1
输出:"aabaa"
提示:
- 0 <= a, b <= 100
- 对于给定的 a 和 b,保证存在满足要求的 s
Code:
class Solution {
public:
string strWithout3a3b(int A, int B) {
//保证A > B
string str = "";
char a = 'a', b = 'b';
if (A < B) {
swap(A, B);
swap(a, b);
}
while (A > 0 || B > 0) {
if (A > 0) {
str.push_back(a);
A --;
}
if (A > B) {
str.push_back(a);
A --;
}
if (B > 0) {
str.push_back(b);
B --;
}
}
return str;
}
};
边栏推荐
- Why does selenium become the first choice for web automated testing? (source code attached)
- MySQL foundation - transaction
- Privacy computing helps secure data circulation and sharing
- 一个简单但是能上分的特征标准化方法
- InheritableThreadLocal 在线程池中进行父子线程间消息传递出现消息丢失的解析
- What are the top level Chinese programmers?
- Accelerate the implementation of intelligent driving projects? You still lack a truth evaluation system
- 毕业生迷茫,中年人焦虑,职场路怎么越走越宽?
- A tour of gRPC:02 - 从proto生成代码
- Flutter技术与实战(1)
猜你喜欢

MySQL进阶——存储引擎

解题元宇宙,网络游戏中的多元通信方案

Comprehensive analysis of Seata distributed transaction at and XA

使用kalibr标定工具进行单目相机和双目相机的标定

MySQL基础——多表查询

After 3 years of testing experience, do you know the state transition method for use case design?

元代理模型可迁移对抗攻击

GNN笔记:消息传播模型

After reading the complete code

毕业生迷茫,中年人焦虑,职场路怎么越走越宽?
随机推荐
kotlin基础语法
XAMPP Apache安装时问题总结
Telnet+ftp to control and upgrade the equipment
Take another picture of cloud redis' improvement path
『计组』CPU 如何区分指令和数据
分片信息调哪个参数呢?用的是MySQLsource stream api,不是table api
C language microblog user management system
PHP delete directory
MATLAB输出格式控制 %d,%f,%c,%s的用法
Science: the interrelated causes and consequences of sleep in the brain
基于opencv进行双目相机的标定
Simulink仿真模式
curl: (56) Recv failure: Connection reset by peer
英联邦国家有哪些
isEmpty 和 isBlank 的用法区别,居然一半的人答不上来?
Practice | extreme optimization of script errors - make script errors clear at a glance
PHP删除目录
What memory consuming data is stored in MySQL CDC jobmanager?
实践 | 脚本错误量极致优化-让脚本错误一目了然
使用kalibr标定工具进行单目相机和双目相机的标定