上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力。

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 'a' letters, and exactly B 'b' letters;
  • The substring 'aaa' does not occur in S;
  • The substring 'bbb' does not occur in S.

Example 1:

Input: A = 1, B = 2
Output: "abb"
Explanation: "abb", "bab" and "bba" are all correct answers.

Example 2:

Input: A = 4, B = 1
Output: "aabaa"

Note:

  1. 0 <= A <= 100
  2. 0 <= B <= 100
  3. It is guaranteed such an S exists for the given A and B.

题目意思:给出A代表a的个数,B代表b的个数,让你制造一个长度为A+B的字符串S,且满足"aaa"和"bbb"不是S的子串。

题目很简单,就是坑点多。我把所有的坑都踩了。下面是代码:

class Solution {
public:
string strWithout3a3b(int A, int B) {
string ans = "";
if( B > A ) {
while( B && A ) {
if( B - A >= && A ) {
ans += "bba";
B -= ;
A -- ;
} else if( B-A && A ) {
ans += "b";
ans += "a";
B --;
A --;
}
}
if( B ) while( B -- ) ans += "b";
if( A ) ans += "a";
} else if( A > B ){
while( B && A ) {
if( A - B >= && B ) {
ans += "aab";
A -= ;
B -- ;
} else if( A-B && B ) {
ans += "a";
ans += "b";
B --;
A --;
}
}
if( A ) while( A -- ) ans += "a";
if( B ) ans += "b";
} else {
while( B ) {
ans += "ab";
B --;
}
}
return ans;
}
};

LeetCode Weekly Contest 121的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. SyntaxError: invalid character in identifier(Python)

    在写博客时直接将博客上的代码复制运行后发现错误SyntaxError: invalid character in identifier,我以为是l(小L)写成了1,改了还是不行. 上网查了下,发现原来 ...

  2. DjangoRestFramework 学习之restful规范 APIview 解析器组件 Postman等

    DjangoRestFramework学习一之restful规范.APIview.解析器组件.Postman等 本节目录 一 预备知识 二 restful规范 三 DRF的APIView和解析器组件 ...

  3. java--List、Set、Map的基础

    好像面试很多面试官都喜欢问这它们的一些问题,所以在这里我稍微总结一下,并把大佬们的文章链接贴在后面. 首先我们借鉴了https://www.cnblogs.com/SnowingYXY/p/67273 ...

  4. go 并发编程(2)

    协程 执行体是个抽象的概念,在操作系统层面有很多个概念与之对应,如操作系统自己掌管的进程(process),进程内的线程(thread),以及进程内的协程(coroutine,也叫轻量级线程).与传统 ...

  5. python-装饰器实现pv-uv

    python-装饰器实现pv-uv   网站流量统计可以帮助我们分析网站的访问和广告来访等数据,里面包含很多数据的,比如访问试用的系统,浏览器,ip归属地,访问时间,搜索引擎来源,广告效果等.原来是一 ...

  6. Django2.0.4 + websocket 实现实时通信,主动推送,聊天室及客服系统

    webSocket是一种在单个TCP连接上进行全双工通信的协议. webSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在WebSocket API中,浏览器 ...

  7. 另存了一次网页之后其它word打开格式都变了

    解决方案: 视图->页面视图 感觉自己很傻...原来另存word为网页后,默认的打开模式就是网页视图了.只需要把视图改回去即可

  8. netcore log4相关

    配置: 1:NuGet程序包 - 搜索log4net - 安装 2:配置代码 Startup文件 #region log4        public static ILoggerRepository ...

  9. MySQL使用root权限创建用户并授权

    MySql篇 1.下载并安装Mysql (1)下载地址 MySQL-8.0下载地址 (2)Mysql配置 1.home目录下命令行执行:vi    .bash_profile来配置MySql绝对路径 ...

  10. 白话skynet第三篇:通过队列解决多线程竞争资源

    今天遇到一个问题,在大厅服务中,如果一个请求使用到了一个公共的变量,如何保证其一致性? 虽然请求是挨个运行的,但是skynet.call会阻塞. "同一个 skynet 服务中的一条消息处理 ...