LeetCode 984.不含AAA或BBB的字符串(C++)
给定两个整数 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 <= 1000 <= B <= 100- 对于给定的
A和B,保证存在满足要求的S。
思路:直观感觉,我们应该先选择当前所剩最多的待写字母写入字符串中。举一个例子,如果 A = 6, B = 2,那么我们期望写出 'aabaabaa'。进一步说,设当前所剩最多的待写字母为 x,只有前两个已经写下的字母都是 x 的时候,下一个写入字符串中的字母才不应该选择它。
class Solution {
public:
string strWithout3a3b(int A, int B) {
string str = "";
char a = 'a', b = 'b';
if (A < B) {
swap(a, b);
swap(A, B);
}
//cout << "A:" << A << " " << a << " B:" << B << " " << b << endl;
while (A > || B > ){
bool flag = false;
if (A - B >= ) {
flag = true;
}
if (A - >= ) {
str.push_back(a);
str.push_back(a);
A = A - ;
}
else if (A == ) {
str.push_back(a);
A--;
}
if (flag) {
str.push_back(b);
B--;
}
else if(B - >= ){
str.push_back(b);
str.push_back(b);
B = B - ;
}
else if (B == ) {
str.push_back(b);
B--;
}
}
return str;
}
};
下面是大哥们的解法:
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 > || B > ) {
if (A > ) {
str.push_back(a);
A --;
}
if (A > B) {
str.push_back(a);
A --;
}
if (B > ) {
str.push_back(b);
B --;
}
}
return str;
}
LeetCode 984.不含AAA或BBB的字符串(C++)的更多相关文章
- [leetcode]984. 不含 AAA 或 BBB 的字符串
给定两个整数 A 和 B,返回任意字符串 S,要求满足: S 的长度为 A + B,且正好包含 A 个 'a' 字母与 B 个 'b' 字母: 子串 'aaa' 没有出现在 S 中: 子串 'bbb' ...
- 【LeetCode】String Without AAA or BBB(不含 AAA 或 BBB 的字符串)
这道题是LeetCode里的第984道题. 题目要求: 给定两个整数 A 和 B,返回任意字符串 S,要求满足: S 的长度为 A + B,且正好包含 A 个 'a' 字母与 B 个 'b' 字母: ...
- [Swift]LeetCode984. 不含 AAA 或 BBB 的字符串 | String Without AAA or BBB
Given two integers A and B, return any string S such that: S has length A + B and contains exactly A ...
- 【LeetCode】984. String Without AAA or BBB 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...
- LC 984. String Without AAA or BBB
Given two integers A and B, return any string S such that: S has length A + B and contains exactly A ...
- 【leetcode】984. String Without AAA or BBB
题目如下: Given two integers A and B, return any string S such that: S has length A + B and contains exa ...
- LeetCode:比较含退格字符串【844】
LeetCode:比较含退格字符串[844] 题目描述 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = ...
- 【Java】 剑指offer(48) 最长不含重复字符的子字符串
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字 ...
- 《剑指offer》第四十八题(最长不含重复字符的子字符串)
// 面试题48:最长不含重复字符的子字符串 // 题目:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子 // 字符串的长度.假设字符串中只包含从'a'到'z'的字符. #inclu ...
随机推荐
- 解决PendingIntent传递参数为空的问题
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0); 在接收端,接收的数据一直为null,在google ...
- 小小c#算法题 - 9 - 基数排序 (Radix Sort)
基数排序和前几篇博客中写到的排序方法完全不同.前面几种排序方法主要是通过关键字间的比较和移动记录这两种操作来实现排序的,而实现基数排序不需要进行记录项间的比较.而是把关键字按一定规则分布在不同的区域, ...
- 具有增删改查功能的表格Demo--【BootStrap】
http://blog.csdn.net/wangmei4968/article/details/48437175
- 遍历一个二维数组的简便方法(减少foreach次数)
在一些特定场合可以使用下, 还是有局限性 输出结果 : 另一种场景 : 输出结果 : 更复杂的场景 : 输出结果 :
- MVVM模式WPF的ComboBox数据绑定,使用Dictionary作为数据源
ViewModel//属性定义 Dictionary<int, string> _selGroupList; /// <summary> /// 分组下拉列表 /// < ...
- CS基本网络中Agent使用双网卡进行流量划分
两台服务器,一台服务器作为管理节点[单网卡],一台服务器作为计算节点[双网卡] ------------------------------------------------------------ ...
- 【转】php通过curl跨域向asp.net服务器上传文件及参数
转:http://blog.sina.com.cn/s/blog_13331dce50102vq32.html 这是一个由php通过调用asp.net接口向asp.net服务器post上传文件及参数并 ...
- 亿级PV请求的三种负载均衡技术(转)
http://www.360doc.com/content/17/1126/23/50145453_707419125.shtml 目录 DNS轮询 LVS负载均衡 DR模式 NAT模式 ...
- Python发送邮件代码
Python发送带附件的邮件代码 #coding: utf-8 import smtplib import sys import datetime from email.mime.text impor ...
- Linux中Mysql安装卸载
参考博客:https://www.cnblogs.com/xrog/p/6862669.html安装步骤:#wget http://dev.mysql.com/get/mysql57-communit ...