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

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. linux 标注主机别名

    vi /etc/bashrc # Turn on checkwinsize  shopt -s checkwinsize  [ "$PS1" = "\\s-\\v\\\$ ...

  2. archlinux下我的软件列表

    编辑器:leafpad,gvim 终端:zsh,xfce4-terminal 音乐:网易云音乐(良心),DeadBeef(和win的foobar2000差不多) 视频播放:mpv PDF:Acrore ...

  3. github=>git=>composer Packages 使用教程

    2018年12月17日14:32:05 因为要做搜索,所以需要用分词工具php的分词不借助的第三方的真的很少, 目前选择的是 http://www.phpbone.com/phpanalysis/ 但 ...

  4. 长时间关机测试脚本.VBS

    Sub Main Dim cnt Dim delay Dim time Dim atttime atttime = 20 delay = 3000 time = 50 cnt_time=3 crt.s ...

  5. Cutting Codeforces Round #493 (Div. 2)

    Cutting There are a lot of things which could be cut — trees, paper, “the rope”. In this problem you ...

  6. ACL(Access Control List)

    一.ACL的简介 ACL(Access Control List 访问控制列表)是路由器和交换机接口的指令列表,用来控制端口进出的数据包.ACL的定义也是基于每一种被动路由协议的,且适用于所有的被动路 ...

  7. OC获取ip地址

    +(NSString *)getIp{ NSError *error;NSURL *ipURL = [NSURL URLWithString:@"http://pv.sohu.com/cit ...

  8. 617A

    #include <stdio.h> int main() { int moves[5]={1,2,3,4,5}; int x; scanf("%d", &x) ...

  9. 【Mac】-NO.133.Mac.1 -【重置忘记macos root密码】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  10. 一HTML基础知识

    网站(前段项目)的目录结构及命名 网站的结构:网站是存放在服务器上的一个文件夹(根目录),是网站所有文件的集合.网站中所有文件按照文件类型或功能分门别类的整理存放. 网站命名规则:网站中的所有文件命名 ...