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

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. vscode + electron 提示:无法连接到legacy请采用inspector解决办法

    首先,你的程序是可以直接运行的,在命令行中可以运行,只是在vsCode中,运行一段时间就被这个提示弹出. 解决方法: 先在launch.json 中加上"protocol":&qu ...

  2. jenkins-参数化构建(二)插件:Extended Choice Parameter

    一.Extended Choice Parameter插件 这个插件相对丰富,安装过程就不过多介绍了,在点击项目设置后会出现下载的插件名字. 写在文件中构建时效果如下:

  3. 转载Alpine Linux常用命令

    Alpine Linux常用命令 目录 一:Alpine Linux开启SSH远程登陆 1.简介: 2.配置 3.配置命令 4.重启服务 二:Alpine Linux源管理 1.简介 2.国内源简介: ...

  4. js 讲解

    substring() 取文本中间 split() 分割文本 charcodeat()   utf-8 tolowercase() 小写 正则是一个对象 正则   i  不区分大小写 escape(s ...

  5. spring boot application.properties 属性详解

    2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...

  6. linux mysql 定时备份

    1.查看磁盘空间情况: 既然是定时备份,就要选择一个空间充足的磁盘空间,避免出现因空间不足导致备份失败,数据丢失的恶果! 存储到当前磁盘这是最简单,却是最不推荐的:服务器有多块硬盘,最好是把备份存放到 ...

  7. python中关于turtle库的学习笔记

    一.基础概念 1.画布:画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置.常用的画布方法有两个:screensize()和setup(). (1)turtle.screen ...

  8. [Day17]常用API(System、Math、Arrays、BigInteger、BigDecimal)

    1.基本类型包装类 1.1 8种基本类型对应的包装类 字节型 byte Byte 短整型 short Short 整型 int Integer 长整型 long Long 字符型 char Chara ...

  9. synchronized和Lock复习

    刚学编程的时候,不懂得同步的概念,只认为程序按照自己写的顺序执行, 直到学到多线程,但当时理解同步问题,也只是面对临界资源需要加锁去控制, 解决一些,如生产消费的问题.但当时一直没考虑过,多线程的情况 ...

  10. nodejs实时的检测系统文件的变化(无需重启服务)

    1.安装superior npm -g install supervisor 注意 superior必须全局安装,否则错误命令会提示安装到全局 2.修改启动 现在我们需要使用 supervisor a ...